MALI: rockchip: upgrade midgard DDK to r14p0-01rel0
[firefly-linux-kernel-4.4.55.git] / drivers / gpu / arm / midgard / backend / gpu / mali_kbase_mmu_hw_direct.c
1 /*
2  *
3  * (C) COPYRIGHT 2014-2016 ARM Limited. All rights reserved.
4  *
5  * This program is free software and is provided to you under the terms of the
6  * GNU General Public License version 2 as published by the Free Software
7  * Foundation, and any use by you of this program is subject to the terms
8  * of such GNU licence.
9  *
10  * A copy of the licence is included with the program, and can also be obtained
11  * from Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
12  * Boston, MA  02110-1301, USA.
13  *
14  */
15
16
17
18 #include <linux/bitops.h>
19
20 #include <mali_kbase.h>
21 #include <mali_kbase_mem.h>
22 #include <mali_kbase_mmu_hw.h>
23 #include <mali_kbase_tlstream.h>
24 #include <backend/gpu/mali_kbase_device_internal.h>
25 #include <mali_kbase_as_fault_debugfs.h>
26
27 static inline u64 lock_region(struct kbase_device *kbdev, u64 pfn,
28                 u32 num_pages)
29 {
30         u64 region;
31
32         /* can't lock a zero sized range */
33         KBASE_DEBUG_ASSERT(num_pages);
34
35         region = pfn << PAGE_SHIFT;
36         /*
37          * fls returns (given the ASSERT above):
38          * 1 .. 32
39          *
40          * 10 + fls(num_pages)
41          * results in the range (11 .. 42)
42          */
43
44         /* gracefully handle num_pages being zero */
45         if (0 == num_pages) {
46                 region |= 11;
47         } else {
48                 u8 region_width;
49
50                 region_width = 10 + fls(num_pages);
51                 if (num_pages != (1ul << (region_width - 11))) {
52                         /* not pow2, so must go up to the next pow2 */
53                         region_width += 1;
54                 }
55                 KBASE_DEBUG_ASSERT(region_width <= KBASE_LOCK_REGION_MAX_SIZE);
56                 KBASE_DEBUG_ASSERT(region_width >= KBASE_LOCK_REGION_MIN_SIZE);
57                 region |= region_width;
58         }
59
60         return region;
61 }
62
63 static int wait_ready(struct kbase_device *kbdev,
64                 unsigned int as_nr, struct kbase_context *kctx)
65 {
66         unsigned int max_loops = KBASE_AS_INACTIVE_MAX_LOOPS;
67         u32 val = kbase_reg_read(kbdev, MMU_AS_REG(as_nr, AS_STATUS), kctx);
68
69         /* Wait for the MMU status to indicate there is no active command, in
70          * case one is pending. Do not log remaining register accesses. */
71         while (--max_loops && (val & AS_STATUS_AS_ACTIVE))
72                 val = kbase_reg_read(kbdev, MMU_AS_REG(as_nr, AS_STATUS), NULL);
73
74         if (max_loops == 0) {
75                 dev_err(kbdev->dev, "AS_ACTIVE bit stuck\n");
76                 return -1;
77         }
78
79         /* If waiting in loop was performed, log last read value. */
80         if (KBASE_AS_INACTIVE_MAX_LOOPS - 1 > max_loops)
81                 kbase_reg_read(kbdev, MMU_AS_REG(as_nr, AS_STATUS), kctx);
82
83         return 0;
84 }
85
86 static int write_cmd(struct kbase_device *kbdev, int as_nr, u32 cmd,
87                 struct kbase_context *kctx)
88 {
89         int status;
90
91         /* write AS_COMMAND when MMU is ready to accept another command */
92         status = wait_ready(kbdev, as_nr, kctx);
93         if (status == 0)
94                 kbase_reg_write(kbdev, MMU_AS_REG(as_nr, AS_COMMAND), cmd,
95                                                                         kctx);
96
97         return status;
98 }
99
100 static void validate_protected_page_fault(struct kbase_device *kbdev,
101                 struct kbase_context *kctx)
102 {
103         /* GPUs which support (native) protected mode shall not report page
104          * fault addresses unless it has protected debug mode and protected
105          * debug mode is turned on */
106         u32 protected_debug_mode = 0;
107
108         if (!kbase_hw_has_feature(kbdev, BASE_HW_FEATURE_PROTECTED_MODE))
109                 return;
110
111         if (kbase_hw_has_feature(kbdev, BASE_HW_FEATURE_PROTECTED_DEBUG_MODE)) {
112                 protected_debug_mode = kbase_reg_read(kbdev,
113                                 GPU_CONTROL_REG(GPU_STATUS),
114                                 kctx) & GPU_DBGEN;
115         }
116
117         if (!protected_debug_mode) {
118                 /* fault_addr should never be reported in protected mode.
119                  * However, we just continue by printing an error message */
120                 dev_err(kbdev->dev, "Fault address reported in protected mode\n");
121         }
122 }
123
124 void kbase_mmu_interrupt(struct kbase_device *kbdev, u32 irq_stat)
125 {
126         const int num_as = 16;
127         const int busfault_shift = MMU_PAGE_FAULT_FLAGS;
128         const int pf_shift = 0;
129         const unsigned long as_bit_mask = (1UL << num_as) - 1;
130         unsigned long flags;
131         u32 new_mask;
132         u32 tmp;
133
134         /* bus faults */
135         u32 bf_bits = (irq_stat >> busfault_shift) & as_bit_mask;
136         /* page faults (note: Ignore ASes with both pf and bf) */
137         u32 pf_bits = ((irq_stat >> pf_shift) & as_bit_mask) & ~bf_bits;
138
139         KBASE_DEBUG_ASSERT(NULL != kbdev);
140
141         /* remember current mask */
142         spin_lock_irqsave(&kbdev->mmu_mask_change, flags);
143         new_mask = kbase_reg_read(kbdev, MMU_REG(MMU_IRQ_MASK), NULL);
144         /* mask interrupts for now */
145         kbase_reg_write(kbdev, MMU_REG(MMU_IRQ_MASK), 0, NULL);
146         spin_unlock_irqrestore(&kbdev->mmu_mask_change, flags);
147
148         while (bf_bits | pf_bits) {
149                 struct kbase_as *as;
150                 int as_no;
151                 struct kbase_context *kctx;
152
153                 /*
154                  * the while logic ensures we have a bit set, no need to check
155                  * for not-found here
156                  */
157                 as_no = ffs(bf_bits | pf_bits) - 1;
158                 as = &kbdev->as[as_no];
159
160                 /*
161                  * Refcount the kctx ASAP - it shouldn't disappear anyway, since
162                  * Bus/Page faults _should_ only occur whilst jobs are running,
163                  * and a job causing the Bus/Page fault shouldn't complete until
164                  * the MMU is updated
165                  */
166                 kctx = kbasep_js_runpool_lookup_ctx(kbdev, as_no);
167
168
169                 /* find faulting address */
170                 as->fault_addr = kbase_reg_read(kbdev,
171                                                 MMU_AS_REG(as_no,
172                                                         AS_FAULTADDRESS_HI),
173                                                 kctx);
174                 as->fault_addr <<= 32;
175                 as->fault_addr |= kbase_reg_read(kbdev,
176                                                 MMU_AS_REG(as_no,
177                                                         AS_FAULTADDRESS_LO),
178                                                 kctx);
179
180                 /* Mark the fault protected or not */
181                 as->protected_mode = kbdev->protected_mode;
182
183                 if (kbdev->protected_mode && as->fault_addr)
184                 {
185                         /* check if address reporting is allowed */
186                         validate_protected_page_fault(kbdev, kctx);
187                 }
188
189                 /* report the fault to debugfs */
190                 kbase_as_fault_debugfs_new(kbdev, as_no);
191
192                 /* record the fault status */
193                 as->fault_status = kbase_reg_read(kbdev,
194                                                   MMU_AS_REG(as_no,
195                                                         AS_FAULTSTATUS),
196                                                   kctx);
197
198                 /* find the fault type */
199                 as->fault_type = (bf_bits & (1 << as_no)) ?
200                                 KBASE_MMU_FAULT_TYPE_BUS :
201                                 KBASE_MMU_FAULT_TYPE_PAGE;
202
203 #ifdef CONFIG_MALI_GPU_MMU_AARCH64
204                 as->fault_extra_addr = kbase_reg_read(kbdev,
205                                 MMU_AS_REG(as_no, AS_FAULTEXTRA_HI),
206                                 kctx);
207                 as->fault_extra_addr <<= 32;
208                 as->fault_extra_addr |= kbase_reg_read(kbdev,
209                                 MMU_AS_REG(as_no, AS_FAULTEXTRA_LO),
210                                 kctx);
211 #endif /* CONFIG_MALI_GPU_MMU_AARCH64 */
212
213                 if (kbase_as_has_bus_fault(as)) {
214                         /* Mark bus fault as handled.
215                          * Note that a bus fault is processed first in case
216                          * where both a bus fault and page fault occur.
217                          */
218                         bf_bits &= ~(1UL << as_no);
219
220                         /* remove the queued BF (and PF) from the mask */
221                         new_mask &= ~(MMU_BUS_ERROR(as_no) |
222                                         MMU_PAGE_FAULT(as_no));
223                 } else {
224                         /* Mark page fault as handled */
225                         pf_bits &= ~(1UL << as_no);
226
227                         /* remove the queued PF from the mask */
228                         new_mask &= ~MMU_PAGE_FAULT(as_no);
229                 }
230
231                 /* Process the interrupt for this address space */
232                 spin_lock_irqsave(&kbdev->hwaccess_lock, flags);
233                 kbase_mmu_interrupt_process(kbdev, kctx, as);
234                 spin_unlock_irqrestore(&kbdev->hwaccess_lock, flags);
235         }
236
237         /* reenable interrupts */
238         spin_lock_irqsave(&kbdev->mmu_mask_change, flags);
239         tmp = kbase_reg_read(kbdev, MMU_REG(MMU_IRQ_MASK), NULL);
240         new_mask |= tmp;
241         kbase_reg_write(kbdev, MMU_REG(MMU_IRQ_MASK), new_mask, NULL);
242         spin_unlock_irqrestore(&kbdev->mmu_mask_change, flags);
243 }
244
245 void kbase_mmu_hw_configure(struct kbase_device *kbdev, struct kbase_as *as,
246                 struct kbase_context *kctx)
247 {
248         struct kbase_mmu_setup *current_setup = &as->current_setup;
249         u32 transcfg = 0;
250
251 #ifdef CONFIG_MALI_GPU_MMU_AARCH64
252         transcfg = current_setup->transcfg & 0xFFFFFFFFUL;
253
254         /* Set flag AS_TRANSCFG_PTW_MEMATTR_WRITE_BACK */
255         /* Clear PTW_MEMATTR bits */
256         transcfg &= ~AS_TRANSCFG_PTW_MEMATTR_MASK;
257         /* Enable correct PTW_MEMATTR bits */
258         transcfg |= AS_TRANSCFG_PTW_MEMATTR_WRITE_BACK;
259
260         if (kbdev->system_coherency == COHERENCY_ACE) {
261                 /* Set flag AS_TRANSCFG_PTW_SH_OS (outer shareable) */
262                 /* Clear PTW_SH bits */
263                 transcfg = (transcfg & ~AS_TRANSCFG_PTW_SH_MASK);
264                 /* Enable correct PTW_SH bits */
265                 transcfg = (transcfg | AS_TRANSCFG_PTW_SH_OS);
266         }
267
268         kbase_reg_write(kbdev, MMU_AS_REG(as->number, AS_TRANSCFG_LO),
269                         transcfg, kctx);
270         kbase_reg_write(kbdev, MMU_AS_REG(as->number, AS_TRANSCFG_HI),
271                         (current_setup->transcfg >> 32) & 0xFFFFFFFFUL, kctx);
272
273 #else /* CONFIG_MALI_GPU_MMU_AARCH64 */
274
275         if (kbdev->system_coherency == COHERENCY_ACE)
276                 current_setup->transtab |= AS_TRANSTAB_LPAE_SHARE_OUTER;
277
278 #endif /* CONFIG_MALI_GPU_MMU_AARCH64 */
279
280         kbase_reg_write(kbdev, MMU_AS_REG(as->number, AS_TRANSTAB_LO),
281                         current_setup->transtab & 0xFFFFFFFFUL, kctx);
282         kbase_reg_write(kbdev, MMU_AS_REG(as->number, AS_TRANSTAB_HI),
283                         (current_setup->transtab >> 32) & 0xFFFFFFFFUL, kctx);
284
285         kbase_reg_write(kbdev, MMU_AS_REG(as->number, AS_MEMATTR_LO),
286                         current_setup->memattr & 0xFFFFFFFFUL, kctx);
287         kbase_reg_write(kbdev, MMU_AS_REG(as->number, AS_MEMATTR_HI),
288                         (current_setup->memattr >> 32) & 0xFFFFFFFFUL, kctx);
289
290         kbase_tlstream_tl_attrib_as_config(as,
291                         current_setup->transtab,
292                         current_setup->memattr,
293                         transcfg);
294
295         write_cmd(kbdev, as->number, AS_COMMAND_UPDATE, kctx);
296 }
297
298 int kbase_mmu_hw_do_operation(struct kbase_device *kbdev, struct kbase_as *as,
299                 struct kbase_context *kctx, u64 vpfn, u32 nr, u32 op,
300                 unsigned int handling_irq)
301 {
302         int ret;
303
304         lockdep_assert_held(&kbdev->mmu_hw_mutex);
305
306         if (op == AS_COMMAND_UNLOCK) {
307                 /* Unlock doesn't require a lock first */
308                 ret = write_cmd(kbdev, as->number, AS_COMMAND_UNLOCK, kctx);
309         } else {
310                 u64 lock_addr = lock_region(kbdev, vpfn, nr);
311
312                 /* Lock the region that needs to be updated */
313                 kbase_reg_write(kbdev, MMU_AS_REG(as->number, AS_LOCKADDR_LO),
314                                 lock_addr & 0xFFFFFFFFUL, kctx);
315                 kbase_reg_write(kbdev, MMU_AS_REG(as->number, AS_LOCKADDR_HI),
316                                 (lock_addr >> 32) & 0xFFFFFFFFUL, kctx);
317                 write_cmd(kbdev, as->number, AS_COMMAND_LOCK, kctx);
318
319                 /* Run the MMU operation */
320                 write_cmd(kbdev, as->number, op, kctx);
321
322                 /* Wait for the flush to complete */
323                 ret = wait_ready(kbdev, as->number, kctx);
324
325                 if (kbase_hw_has_issue(kbdev, BASE_HW_ISSUE_9630)) {
326                         /* Issue an UNLOCK command to ensure that valid page
327                            tables are re-read by the GPU after an update.
328                            Note that, the FLUSH command should perform all the
329                            actions necessary, however the bus logs show that if
330                            multiple page faults occur within an 8 page region
331                            the MMU does not always re-read the updated page
332                            table entries for later faults or is only partially
333                            read, it subsequently raises the page fault IRQ for
334                            the same addresses, the unlock ensures that the MMU
335                            cache is flushed, so updates can be re-read.  As the
336                            region is now unlocked we need to issue 2 UNLOCK
337                            commands in order to flush the MMU/uTLB,
338                            see PRLAM-8812.
339                          */
340                         write_cmd(kbdev, as->number, AS_COMMAND_UNLOCK, kctx);
341                         write_cmd(kbdev, as->number, AS_COMMAND_UNLOCK, kctx);
342                 }
343         }
344
345         return ret;
346 }
347
348 void kbase_mmu_hw_clear_fault(struct kbase_device *kbdev, struct kbase_as *as,
349                 struct kbase_context *kctx, enum kbase_mmu_fault_type type)
350 {
351         unsigned long flags;
352         u32 pf_bf_mask;
353
354         spin_lock_irqsave(&kbdev->mmu_mask_change, flags);
355
356         /*
357          * A reset is in-flight and we're flushing the IRQ + bottom half
358          * so don't update anything as it could race with the reset code.
359          */
360         if (kbdev->irq_reset_flush)
361                 goto unlock;
362
363         /* Clear the page (and bus fault IRQ as well in case one occurred) */
364         pf_bf_mask = MMU_PAGE_FAULT(as->number);
365         if (type == KBASE_MMU_FAULT_TYPE_BUS ||
366                         type == KBASE_MMU_FAULT_TYPE_BUS_UNEXPECTED)
367                 pf_bf_mask |= MMU_BUS_ERROR(as->number);
368
369         kbase_reg_write(kbdev, MMU_REG(MMU_IRQ_CLEAR), pf_bf_mask, kctx);
370
371 unlock:
372         spin_unlock_irqrestore(&kbdev->mmu_mask_change, flags);
373 }
374
375 void kbase_mmu_hw_enable_fault(struct kbase_device *kbdev, struct kbase_as *as,
376                 struct kbase_context *kctx, enum kbase_mmu_fault_type type)
377 {
378         unsigned long flags;
379         u32 irq_mask;
380
381         /* Enable the page fault IRQ (and bus fault IRQ as well in case one
382          * occurred) */
383         spin_lock_irqsave(&kbdev->mmu_mask_change, flags);
384
385         /*
386          * A reset is in-flight and we're flushing the IRQ + bottom half
387          * so don't update anything as it could race with the reset code.
388          */
389         if (kbdev->irq_reset_flush)
390                 goto unlock;
391
392         irq_mask = kbase_reg_read(kbdev, MMU_REG(MMU_IRQ_MASK), kctx) |
393                         MMU_PAGE_FAULT(as->number);
394
395         if (type == KBASE_MMU_FAULT_TYPE_BUS ||
396                         type == KBASE_MMU_FAULT_TYPE_BUS_UNEXPECTED)
397                 irq_mask |= MMU_BUS_ERROR(as->number);
398
399         kbase_reg_write(kbdev, MMU_REG(MMU_IRQ_MASK), irq_mask, kctx);
400
401 unlock:
402         spin_unlock_irqrestore(&kbdev->mmu_mask_change, flags);
403 }