4fd13e2de63e317679929feb9736e6a28a407743
[firefly-linux-kernel-4.4.55.git] / drivers / gpu / arm / midgard_for_linux / backend / gpu / mali_kbase_mmu_hw_direct.c
1 /*
2  *
3  * (C) COPYRIGHT 2014-2015 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 #if defined(CONFIG_MALI_MIPE_ENABLED)
24 #include <mali_kbase_tlstream.h>
25 #endif
26 #include <backend/gpu/mali_kbase_mmu_hw_direct.h>
27 #include <backend/gpu/mali_kbase_device_internal.h>
28
29 static inline u64 lock_region(struct kbase_device *kbdev, u64 pfn,
30                 u32 num_pages)
31 {
32         u64 region;
33
34         /* can't lock a zero sized range */
35         KBASE_DEBUG_ASSERT(num_pages);
36
37         region = pfn << PAGE_SHIFT;
38         /*
39          * fls returns (given the ASSERT above):
40          * 1 .. 32
41          *
42          * 10 + fls(num_pages)
43          * results in the range (11 .. 42)
44          */
45
46         /* gracefully handle num_pages being zero */
47         if (0 == num_pages) {
48                 region |= 11;
49         } else {
50                 u8 region_width;
51
52                 region_width = 10 + fls(num_pages);
53                 if (num_pages != (1ul << (region_width - 11))) {
54                         /* not pow2, so must go up to the next pow2 */
55                         region_width += 1;
56                 }
57                 KBASE_DEBUG_ASSERT(region_width <= KBASE_LOCK_REGION_MAX_SIZE);
58                 KBASE_DEBUG_ASSERT(region_width >= KBASE_LOCK_REGION_MIN_SIZE);
59                 region |= region_width;
60         }
61
62         return region;
63 }
64
65 static int wait_ready(struct kbase_device *kbdev,
66                 unsigned int as_nr, struct kbase_context *kctx)
67 {
68         unsigned int max_loops = KBASE_AS_INACTIVE_MAX_LOOPS;
69         u32 val = kbase_reg_read(kbdev, MMU_AS_REG(as_nr, AS_STATUS), kctx);
70
71         /* Wait for the MMU status to indicate there is no active command, in
72          * case one is pending. Do not log remaining register accesses. */
73         while (--max_loops && (val & AS_STATUS_AS_ACTIVE))
74                 val = kbase_reg_read(kbdev, MMU_AS_REG(as_nr, AS_STATUS), NULL);
75
76         if (max_loops == 0) {
77                 dev_err(kbdev->dev, "AS_ACTIVE bit stuck\n");
78                 return -1;
79         }
80
81         /* If waiting in loop was performed, log last read value. */
82         if (KBASE_AS_INACTIVE_MAX_LOOPS - 1 > max_loops)
83                 kbase_reg_read(kbdev, MMU_AS_REG(as_nr, AS_STATUS), kctx);
84
85         return 0;
86 }
87
88 static int write_cmd(struct kbase_device *kbdev, int as_nr, u32 cmd,
89                 struct kbase_context *kctx)
90 {
91         int status;
92
93         /* write AS_COMMAND when MMU is ready to accept another command */
94         status = wait_ready(kbdev, as_nr, kctx);
95         if (status == 0)
96                 kbase_reg_write(kbdev, MMU_AS_REG(as_nr, AS_COMMAND), cmd,
97                                                                         kctx);
98
99         return status;
100 }
101
102 void kbase_mmu_interrupt(struct kbase_device *kbdev, u32 irq_stat)
103 {
104         const int num_as = 16;
105         const int busfault_shift = MMU_PAGE_FAULT_FLAGS;
106         const int pf_shift = 0;
107         const unsigned long as_bit_mask = (1UL << num_as) - 1;
108         unsigned long flags;
109         u32 new_mask;
110         u32 tmp;
111
112         /* bus faults */
113         u32 bf_bits = (irq_stat >> busfault_shift) & as_bit_mask;
114         /* page faults (note: Ignore ASes with both pf and bf) */
115         u32 pf_bits = ((irq_stat >> pf_shift) & as_bit_mask) & ~bf_bits;
116
117         KBASE_DEBUG_ASSERT(NULL != kbdev);
118
119         /* remember current mask */
120         spin_lock_irqsave(&kbdev->mmu_mask_change, flags);
121         new_mask = kbase_reg_read(kbdev, MMU_REG(MMU_IRQ_MASK), NULL);
122         /* mask interrupts for now */
123         kbase_reg_write(kbdev, MMU_REG(MMU_IRQ_MASK), 0, NULL);
124         spin_unlock_irqrestore(&kbdev->mmu_mask_change, flags);
125
126         while (bf_bits | pf_bits) {
127                 struct kbase_as *as;
128                 int as_no;
129                 struct kbase_context *kctx;
130
131                 /*
132                  * the while logic ensures we have a bit set, no need to check
133                  * for not-found here
134                  */
135                 as_no = ffs(bf_bits | pf_bits) - 1;
136                 as = &kbdev->as[as_no];
137
138                 /*
139                  * Refcount the kctx ASAP - it shouldn't disappear anyway, since
140                  * Bus/Page faults _should_ only occur whilst jobs are running,
141                  * and a job causing the Bus/Page fault shouldn't complete until
142                  * the MMU is updated
143                  */
144                 kctx = kbasep_js_runpool_lookup_ctx(kbdev, as_no);
145
146                 /* find faulting address */
147                 as->fault_addr = kbase_reg_read(kbdev,
148                                                 MMU_AS_REG(as_no,
149                                                         AS_FAULTADDRESS_HI),
150                                                 kctx);
151                 as->fault_addr <<= 32;
152                 as->fault_addr |= kbase_reg_read(kbdev,
153                                                 MMU_AS_REG(as_no,
154                                                         AS_FAULTADDRESS_LO),
155                                                 kctx);
156
157                 /* record the fault status */
158                 as->fault_status = kbase_reg_read(kbdev,
159                                                   MMU_AS_REG(as_no,
160                                                         AS_FAULTSTATUS),
161                                                   kctx);
162
163                 /* find the fault type */
164                 as->fault_type = (bf_bits & (1 << as_no)) ?
165                                 KBASE_MMU_FAULT_TYPE_BUS :
166                                 KBASE_MMU_FAULT_TYPE_PAGE;
167
168
169                 if (kbase_as_has_bus_fault(as)) {
170                         /* Mark bus fault as handled.
171                          * Note that a bus fault is processed first in case
172                          * where both a bus fault and page fault occur.
173                          */
174                         bf_bits &= ~(1UL << as_no);
175
176                         /* remove the queued BF (and PF) from the mask */
177                         new_mask &= ~(MMU_BUS_ERROR(as_no) |
178                                         MMU_PAGE_FAULT(as_no));
179                 } else {
180                         /* Mark page fault as handled */
181                         pf_bits &= ~(1UL << as_no);
182
183                         /* remove the queued PF from the mask */
184                         new_mask &= ~MMU_PAGE_FAULT(as_no);
185                 }
186
187                 /* Process the interrupt for this address space */
188                 spin_lock_irqsave(&kbdev->js_data.runpool_irq.lock, flags);
189                 kbase_mmu_interrupt_process(kbdev, kctx, as);
190                 spin_unlock_irqrestore(&kbdev->js_data.runpool_irq.lock,
191                                 flags);
192         }
193
194         /* reenable interrupts */
195         spin_lock_irqsave(&kbdev->mmu_mask_change, flags);
196         tmp = kbase_reg_read(kbdev, MMU_REG(MMU_IRQ_MASK), NULL);
197         new_mask |= tmp;
198         kbase_reg_write(kbdev, MMU_REG(MMU_IRQ_MASK), new_mask, NULL);
199         spin_unlock_irqrestore(&kbdev->mmu_mask_change, flags);
200 }
201
202 void kbase_mmu_hw_configure(struct kbase_device *kbdev, struct kbase_as *as,
203                 struct kbase_context *kctx)
204 {
205         struct kbase_mmu_setup *current_setup = &as->current_setup;
206 #ifdef CONFIG_MALI_MIPE_ENABLED
207         u32 transcfg = 0;
208 #endif
209
210
211         kbase_reg_write(kbdev, MMU_AS_REG(as->number, AS_TRANSTAB_LO),
212                         current_setup->transtab & 0xFFFFFFFFUL, kctx);
213         kbase_reg_write(kbdev, MMU_AS_REG(as->number, AS_TRANSTAB_HI),
214                         (current_setup->transtab >> 32) & 0xFFFFFFFFUL, kctx);
215
216         kbase_reg_write(kbdev, MMU_AS_REG(as->number, AS_MEMATTR_LO),
217                         current_setup->memattr & 0xFFFFFFFFUL, kctx);
218         kbase_reg_write(kbdev, MMU_AS_REG(as->number, AS_MEMATTR_HI),
219                         (current_setup->memattr >> 32) & 0xFFFFFFFFUL, kctx);
220
221 #if defined(CONFIG_MALI_MIPE_ENABLED)
222         kbase_tlstream_tl_attrib_as_config(as,
223                         current_setup->transtab,
224                         current_setup->memattr,
225                         transcfg);
226 #endif
227
228         write_cmd(kbdev, as->number, AS_COMMAND_UPDATE, kctx);
229 }
230
231 int kbase_mmu_hw_do_operation(struct kbase_device *kbdev, struct kbase_as *as,
232                 struct kbase_context *kctx, u64 vpfn, u32 nr, u32 op,
233                 unsigned int handling_irq)
234 {
235         int ret;
236
237         if (op == AS_COMMAND_UNLOCK) {
238                 /* Unlock doesn't require a lock first */
239                 ret = write_cmd(kbdev, as->number, AS_COMMAND_UNLOCK, kctx);
240         } else {
241                 u64 lock_addr = lock_region(kbdev, vpfn, nr);
242
243                 /* Lock the region that needs to be updated */
244                 kbase_reg_write(kbdev, MMU_AS_REG(as->number, AS_LOCKADDR_LO),
245                                 lock_addr & 0xFFFFFFFFUL, kctx);
246                 kbase_reg_write(kbdev, MMU_AS_REG(as->number, AS_LOCKADDR_HI),
247                                 (lock_addr >> 32) & 0xFFFFFFFFUL, kctx);
248                 write_cmd(kbdev, as->number, AS_COMMAND_LOCK, kctx);
249
250                 /* Run the MMU operation */
251                 write_cmd(kbdev, as->number, op, kctx);
252
253                 /* Wait for the flush to complete */
254                 ret = wait_ready(kbdev, as->number, kctx);
255
256                 if (kbase_hw_has_issue(kbdev, BASE_HW_ISSUE_9630)) {
257                         /* Issue an UNLOCK command to ensure that valid page
258                            tables are re-read by the GPU after an update.
259                            Note that, the FLUSH command should perform all the
260                            actions necessary, however the bus logs show that if
261                            multiple page faults occur within an 8 page region
262                            the MMU does not always re-read the updated page
263                            table entries for later faults or is only partially
264                            read, it subsequently raises the page fault IRQ for
265                            the same addresses, the unlock ensures that the MMU
266                            cache is flushed, so updates can be re-read.  As the
267                            region is now unlocked we need to issue 2 UNLOCK
268                            commands in order to flush the MMU/uTLB,
269                            see PRLAM-8812.
270                          */
271                         write_cmd(kbdev, as->number, AS_COMMAND_UNLOCK, kctx);
272                         write_cmd(kbdev, as->number, AS_COMMAND_UNLOCK, kctx);
273                 }
274         }
275
276         return ret;
277 }
278
279 void kbase_mmu_hw_clear_fault(struct kbase_device *kbdev, struct kbase_as *as,
280                 struct kbase_context *kctx, enum kbase_mmu_fault_type type)
281 {
282         unsigned long flags;
283         u32 pf_bf_mask;
284
285         spin_lock_irqsave(&kbdev->mmu_mask_change, flags);
286
287         /*
288          * A reset is in-flight and we're flushing the IRQ + bottom half
289          * so don't update anything as it could race with the reset code.
290          */
291         if (kbdev->irq_reset_flush)
292                 goto unlock;
293
294         /* Clear the page (and bus fault IRQ as well in case one occurred) */
295         pf_bf_mask = MMU_PAGE_FAULT(as->number);
296         if (type == KBASE_MMU_FAULT_TYPE_BUS ||
297                         type == KBASE_MMU_FAULT_TYPE_BUS_UNEXPECTED)
298                 pf_bf_mask |= MMU_BUS_ERROR(as->number);
299
300         kbase_reg_write(kbdev, MMU_REG(MMU_IRQ_CLEAR), pf_bf_mask, kctx);
301
302 unlock:
303         spin_unlock_irqrestore(&kbdev->mmu_mask_change, flags);
304 }
305
306 void kbase_mmu_hw_enable_fault(struct kbase_device *kbdev, struct kbase_as *as,
307                 struct kbase_context *kctx, enum kbase_mmu_fault_type type)
308 {
309         unsigned long flags;
310         u32 irq_mask;
311
312         /* Enable the page fault IRQ (and bus fault IRQ as well in case one
313          * occurred) */
314         spin_lock_irqsave(&kbdev->mmu_mask_change, flags);
315
316         /*
317          * A reset is in-flight and we're flushing the IRQ + bottom half
318          * so don't update anything as it could race with the reset code.
319          */
320         if (kbdev->irq_reset_flush)
321                 goto unlock;
322
323         irq_mask = kbase_reg_read(kbdev, MMU_REG(MMU_IRQ_MASK), kctx) |
324                         MMU_PAGE_FAULT(as->number);
325
326         if (type == KBASE_MMU_FAULT_TYPE_BUS ||
327                         type == KBASE_MMU_FAULT_TYPE_BUS_UNEXPECTED)
328                 irq_mask |= MMU_BUS_ERROR(as->number);
329
330         kbase_reg_write(kbdev, MMU_REG(MMU_IRQ_MASK), irq_mask, kctx);
331
332 unlock:
333         spin_unlock_irqrestore(&kbdev->mmu_mask_change, flags);
334 }