Merge branch develop-3.10 into develop-3.10-next
[firefly-linux-kernel-4.4.55.git] / drivers / gpu / arm / midgard / mali_kbase_mmu_hw_direct.c
1 /*
2  *
3  * (C) COPYRIGHT 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_mmu_hw_direct.h>
24
25 #if KBASE_MMU_HW_BACKEND
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
68         /* Wait for the MMU status to indicate there is no active command. */
69         while (--max_loops && kbase_reg_read(kbdev,
70                         MMU_AS_REG(as_nr, AS_STATUS),
71                         kctx) & AS_STATUS_AS_ACTIVE) {
72                 ;
73         }
74
75         if (max_loops == 0) {
76                 dev_err(kbdev->dev, "AS_ACTIVE bit stuck\n");
77                 return -1;
78         }
79
80         return 0;
81 }
82
83 static int write_cmd(struct kbase_device *kbdev, int as_nr, u32 cmd,
84                 struct kbase_context *kctx)
85 {
86         int status;
87
88         /* write AS_COMMAND when MMU is ready to accept another command */
89         status = wait_ready(kbdev, as_nr, kctx);
90         if (status == 0)
91                 kbase_reg_write(kbdev, MMU_AS_REG(as_nr, AS_COMMAND), cmd, kctx);
92
93         return status;
94 }
95
96 void kbase_mmu_interrupt(struct kbase_device *kbdev, u32 irq_stat)
97 {
98         const int num_as = 16;
99         const int busfault_shift = MMU_REGS_PAGE_FAULT_FLAGS;
100         const int pf_shift = 0;
101         const unsigned long as_bit_mask = (1UL << num_as) - 1;
102         unsigned long flags;
103         u32 new_mask;
104         u32 tmp;
105
106         /* bus faults */
107         u32 bf_bits = (irq_stat >> busfault_shift) & as_bit_mask;
108         /* page faults (note: Ignore ASes with both pf and bf) */
109         u32 pf_bits = ((irq_stat >> pf_shift) & as_bit_mask) & ~bf_bits;
110
111         KBASE_DEBUG_ASSERT(NULL != kbdev);
112
113         /* remember current mask */
114         spin_lock_irqsave(&kbdev->mmu_mask_change, flags);
115         new_mask = kbase_reg_read(kbdev, MMU_REG(MMU_IRQ_MASK), NULL);
116         /* mask interrupts for now */
117         kbase_reg_write(kbdev, MMU_REG(MMU_IRQ_MASK), 0, NULL);
118         spin_unlock_irqrestore(&kbdev->mmu_mask_change, flags);
119
120         while (bf_bits | pf_bits) {
121                 struct kbase_as *as;
122                 int as_no;
123                 struct kbase_context *kctx;
124
125                 /*
126                  * the while logic ensures we have a bit set, no need to check
127                  * for not-found here
128                  */
129                 as_no = ffs(bf_bits | pf_bits) - 1;
130                 as = &kbdev->as[as_no];
131
132                 /*
133                  * Refcount the kctx ASAP - it shouldn't disappear anyway, since
134                  * Bus/Page faults _should_ only occur whilst jobs are running,
135                  * and a job causing the Bus/Page fault shouldn't complete until
136                  * the MMU is updated
137                  */
138                 kctx = kbasep_js_runpool_lookup_ctx(kbdev, as_no);
139
140                 /* find faulting address */
141                 as->fault_addr = kbase_reg_read(kbdev,
142                                                 MMU_AS_REG(as_no, AS_FAULTADDRESS_HI),
143                                                 kctx);
144                 as->fault_addr <<= 32;
145                 as->fault_addr |= kbase_reg_read(kbdev,
146                                                 MMU_AS_REG(as_no, AS_FAULTADDRESS_LO),
147                                                 kctx);
148
149                 /* record the fault status */
150                 as->fault_status = kbase_reg_read(kbdev,
151                                                   MMU_AS_REG(as_no, AS_FAULTSTATUS),
152                                                   kctx);
153
154                 /* find the fault type */
155                 as->fault_type = (bf_bits & (1 << as_no)) ?
156                                   KBASE_MMU_FAULT_TYPE_BUS : KBASE_MMU_FAULT_TYPE_PAGE;
157
158
159                 if (kbase_as_has_bus_fault(as)) {
160                         /*
161                          * Clear the internal JM mask first before clearing the
162                          * internal MMU mask
163                          *
164                          * Note:
165                          * Always clear the page fault just in case there was
166                          * one at the same time as the bus error (bus errors are
167                          * always processed in preference to pagefaults should
168                          * both happen at the same time).
169                          */
170                         kbase_reg_write(kbdev, MMU_REG(MMU_IRQ_CLEAR),
171                                         (1UL << MMU_REGS_BUS_ERROR_FLAG(as_no)) |
172                                         (1UL << MMU_REGS_PAGE_FAULT_FLAG(as_no)), kctx);
173
174                         /* mark as handled (note: bf_bits is already shifted) */
175                         bf_bits &= ~(1UL << (as_no));
176
177                         /* remove the queued BFs (and PFs) from the mask */
178                         new_mask &= ~((1UL << MMU_REGS_BUS_ERROR_FLAG(as_no)) |
179                                       (1UL << MMU_REGS_PAGE_FAULT_FLAG(as_no)));
180                 } else {
181                         /*
182                          * Clear the internal JM mask first before clearing the
183                          * internal MMU mask
184                          */
185                         kbase_reg_write(kbdev, MMU_REG(MMU_IRQ_CLEAR),
186                                         1UL << MMU_REGS_PAGE_FAULT_FLAG(as_no),
187                                         kctx);
188
189                         /* mark as handled */
190                         pf_bits &= ~(1UL << as_no);
191
192                         /* remove the queued PFs from the mask */
193                         new_mask &= ~(1UL << MMU_REGS_PAGE_FAULT_FLAG(as_no));
194                 }
195
196                 /* Process the interrupt for this address space */
197                 kbase_mmu_interrupt_process(kbdev, kctx, as);
198         }
199
200         /* reenable interrupts */
201         spin_lock_irqsave(&kbdev->mmu_mask_change, flags);
202         tmp = kbase_reg_read(kbdev, MMU_REG(MMU_IRQ_MASK), NULL);
203         new_mask |= tmp;
204         kbase_reg_write(kbdev, MMU_REG(MMU_IRQ_MASK), new_mask, NULL);
205         spin_unlock_irqrestore(&kbdev->mmu_mask_change, flags);
206 }
207
208 void kbase_mmu_hw_configure(struct kbase_device *kbdev, struct kbase_as *as,
209                 struct kbase_context *kctx)
210 {
211         struct kbase_mmu_setup *current_setup = &as->current_setup;
212
213         kbase_reg_write(kbdev, MMU_AS_REG(as->number, AS_TRANSTAB_LO),
214                         current_setup->transtab & 0xFFFFFFFFUL, kctx);
215         kbase_reg_write(kbdev, MMU_AS_REG(as->number, AS_TRANSTAB_HI),
216                         (current_setup->transtab >> 32) & 0xFFFFFFFFUL, kctx);
217
218         kbase_reg_write(kbdev, MMU_AS_REG(as->number, AS_MEMATTR_LO),
219                         current_setup->memattr & 0xFFFFFFFFUL, kctx);
220         kbase_reg_write(kbdev, MMU_AS_REG(as->number, AS_MEMATTR_HI),
221                         (current_setup->memattr >> 32) & 0xFFFFFFFFUL, kctx);
222         write_cmd(kbdev, as->number, AS_COMMAND_UPDATE, kctx);
223 }
224
225 int kbase_mmu_hw_do_operation(struct kbase_device *kbdev, struct kbase_as *as,
226                 struct kbase_context *kctx, u64 vpfn, u32 nr, u32 op,
227                 unsigned int handling_irq)
228 {
229         int ret;
230
231         if (op == AS_COMMAND_UNLOCK) {
232                 /* Unlock doesn't require a lock first */
233                 ret = write_cmd(kbdev, as->number, AS_COMMAND_UNLOCK, kctx);
234         } else {
235                 u64 lock_addr = lock_region(kbdev, vpfn, nr);
236
237                 /* Lock the region that needs to be updated */
238                 kbase_reg_write(kbdev, MMU_AS_REG(as->number, AS_LOCKADDR_LO),
239                                 lock_addr & 0xFFFFFFFFUL, kctx);
240                 kbase_reg_write(kbdev, MMU_AS_REG(as->number, AS_LOCKADDR_HI),
241                                 (lock_addr >> 32) & 0xFFFFFFFFUL, kctx);
242                 write_cmd(kbdev, as->number, AS_COMMAND_LOCK, kctx);
243
244                 if (kbase_hw_has_issue(kbdev, BASE_HW_ISSUE_T76X_3285) &&
245                                 handling_irq) {
246                         kbase_reg_write(kbdev, MMU_REG(MMU_IRQ_CLEAR),
247                                         (1UL << as->number), NULL);
248                         write_cmd(kbdev, as->number, AS_COMMAND_LOCK, kctx);
249                 }
250
251                 /* Run the MMU operation */
252                 write_cmd(kbdev, as->number, op, kctx);
253
254                 /* Wait for the flush to complete */
255                 ret = wait_ready(kbdev, as->number, kctx);
256
257                 if (kbase_hw_has_issue(kbdev, BASE_HW_ISSUE_9630)) {
258                         /* Issue an UNLOCK command to ensure that valid page
259                            tables are re-read by the GPU after an update.
260                            Note that, the FLUSH command should perform all the
261                            actions necessary, however the bus logs show that if
262                            multiple page faults occur within an 8 page region
263                            the MMU does not always re-read the updated page
264                            table entries for later faults or is only partially
265                            read, it subsequently raises the page fault IRQ for
266                            the same addresses, the unlock ensures that the MMU
267                            cache is flushed, so updates can be re-read.  As the
268                            region is now unlocked we need to issue 2 UNLOCK
269                            commands in order to flush the MMU/uTLB,
270                            see PRLAM-8812.
271                          */
272                         write_cmd(kbdev, as->number, AS_COMMAND_UNLOCK, kctx);
273                         write_cmd(kbdev, as->number, AS_COMMAND_UNLOCK, kctx);
274                 }
275         }
276
277         return ret;
278 }
279
280 void kbase_mmu_hw_clear_fault(struct kbase_device *kbdev, struct kbase_as *as,
281                 struct kbase_context *kctx, enum kbase_mmu_fault_type type)
282 {
283         unsigned long flags;
284         u32 mask;
285
286         spin_lock_irqsave(&kbdev->mmu_mask_change, flags);
287         mask = kbase_reg_read(kbdev, MMU_REG(MMU_IRQ_MASK), kctx);
288
289         mask |= (1UL << MMU_REGS_PAGE_FAULT_FLAG(as->number));
290         if (type == KBASE_MMU_FAULT_TYPE_BUS)
291                 mask |= (1UL << MMU_REGS_BUS_ERROR_FLAG(as->number));
292
293         kbase_reg_write(kbdev, MMU_REG(MMU_IRQ_MASK), mask, kctx);
294         spin_unlock_irqrestore(&kbdev->mmu_mask_change, flags);
295 }
296 #endif