]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/misc/habanalabs/mmu.c
Merge tag 'armsoc-dt' of git://git.kernel.org/pub/scm/linux/kernel/git/soc/soc
[linux.git] / drivers / misc / habanalabs / mmu.c
1 // SPDX-License-Identifier: GPL-2.0
2
3 /*
4  * Copyright 2016-2019 HabanaLabs, Ltd.
5  * All Rights Reserved.
6  */
7
8 #include "habanalabs.h"
9 #include "include/hw_ip/mmu/mmu_general.h"
10
11 #include <linux/genalloc.h>
12 #include <linux/slab.h>
13
14 static inline u64 get_phys_addr(struct hl_ctx *ctx, u64 shadow_addr);
15
16 static struct pgt_info *get_pgt_info(struct hl_ctx *ctx, u64 hop_addr)
17 {
18         struct pgt_info *pgt_info = NULL;
19
20         hash_for_each_possible(ctx->mmu_shadow_hash, pgt_info, node,
21                                 (unsigned long) hop_addr)
22                 if (hop_addr == pgt_info->shadow_addr)
23                         break;
24
25         return pgt_info;
26 }
27
28 static void free_hop(struct hl_ctx *ctx, u64 hop_addr)
29 {
30         struct hl_device *hdev = ctx->hdev;
31         struct pgt_info *pgt_info = get_pgt_info(ctx, hop_addr);
32
33         gen_pool_free(hdev->mmu_pgt_pool, pgt_info->phys_addr,
34                         hdev->asic_prop.mmu_hop_table_size);
35         hash_del(&pgt_info->node);
36         kfree((u64 *) (uintptr_t) pgt_info->shadow_addr);
37         kfree(pgt_info);
38 }
39
40 static u64 alloc_hop(struct hl_ctx *ctx)
41 {
42         struct hl_device *hdev = ctx->hdev;
43         struct asic_fixed_properties *prop = &hdev->asic_prop;
44         struct pgt_info *pgt_info;
45         u64 phys_addr, shadow_addr;
46
47         pgt_info = kmalloc(sizeof(*pgt_info), GFP_KERNEL);
48         if (!pgt_info)
49                 return ULLONG_MAX;
50
51         phys_addr = (u64) gen_pool_alloc(hdev->mmu_pgt_pool,
52                                         prop->mmu_hop_table_size);
53         if (!phys_addr) {
54                 dev_err(hdev->dev, "failed to allocate page\n");
55                 goto pool_add_err;
56         }
57
58         shadow_addr = (u64) (uintptr_t) kzalloc(prop->mmu_hop_table_size,
59                                                 GFP_KERNEL);
60         if (!shadow_addr)
61                 goto shadow_err;
62
63         pgt_info->phys_addr = phys_addr;
64         pgt_info->shadow_addr = shadow_addr;
65         pgt_info->ctx = ctx;
66         pgt_info->num_of_ptes = 0;
67         hash_add(ctx->mmu_shadow_hash, &pgt_info->node, shadow_addr);
68
69         return shadow_addr;
70
71 shadow_err:
72         gen_pool_free(hdev->mmu_pgt_pool, phys_addr, prop->mmu_hop_table_size);
73 pool_add_err:
74         kfree(pgt_info);
75
76         return ULLONG_MAX;
77 }
78
79 static inline u64 get_phys_hop0_addr(struct hl_ctx *ctx)
80 {
81         return ctx->hdev->asic_prop.mmu_pgt_addr +
82                         (ctx->asid * ctx->hdev->asic_prop.mmu_hop_table_size);
83 }
84
85 static inline u64 get_hop0_addr(struct hl_ctx *ctx)
86 {
87         return (u64) (uintptr_t) ctx->hdev->mmu_shadow_hop0 +
88                         (ctx->asid * ctx->hdev->asic_prop.mmu_hop_table_size);
89 }
90
91 static inline void flush(struct hl_ctx *ctx)
92 {
93         /* flush all writes from all cores to reach PCI */
94         mb();
95         ctx->hdev->asic_funcs->read_pte(ctx->hdev, get_phys_hop0_addr(ctx));
96 }
97
98 /* transform the value to physical address when writing to H/W */
99 static inline void write_pte(struct hl_ctx *ctx, u64 shadow_pte_addr, u64 val)
100 {
101         /*
102          * The value to write is actually the address of the next shadow hop +
103          * flags at the 12 LSBs.
104          * Hence in order to get the value to write to the physical PTE, we
105          * clear the 12 LSBs and translate the shadow hop to its associated
106          * physical hop, and add back the original 12 LSBs.
107          */
108         u64 phys_val = get_phys_addr(ctx, val & PTE_PHYS_ADDR_MASK) |
109                                 (val & OFFSET_MASK);
110
111         ctx->hdev->asic_funcs->write_pte(ctx->hdev,
112                                         get_phys_addr(ctx, shadow_pte_addr),
113                                         phys_val);
114
115         *(u64 *) (uintptr_t) shadow_pte_addr = val;
116 }
117
118 /* do not transform the value to physical address when writing to H/W */
119 static inline void write_final_pte(struct hl_ctx *ctx, u64 shadow_pte_addr,
120                                         u64 val)
121 {
122         ctx->hdev->asic_funcs->write_pte(ctx->hdev,
123                                         get_phys_addr(ctx, shadow_pte_addr),
124                                         val);
125         *(u64 *) (uintptr_t) shadow_pte_addr = val;
126 }
127
128 /* clear the last and present bits */
129 static inline void clear_pte(struct hl_ctx *ctx, u64 pte_addr)
130 {
131         /* no need to transform the value to physical address */
132         write_final_pte(ctx, pte_addr, 0);
133 }
134
135 static inline void get_pte(struct hl_ctx *ctx, u64 hop_addr)
136 {
137         get_pgt_info(ctx, hop_addr)->num_of_ptes++;
138 }
139
140 /*
141  * put_pte - decrement the num of ptes and free the hop if possible
142  *
143  * @ctx: pointer to the context structure
144  * @hop_addr: addr of the hop
145  *
146  * This function returns the number of ptes left on this hop. If the number is
147  * 0, it means the pte was freed.
148  */
149 static inline int put_pte(struct hl_ctx *ctx, u64 hop_addr)
150 {
151         struct pgt_info *pgt_info = get_pgt_info(ctx, hop_addr);
152         int num_of_ptes_left;
153
154         pgt_info->num_of_ptes--;
155
156         /*
157          * Need to save the number of ptes left because free_hop might free
158          * the pgt_info
159          */
160         num_of_ptes_left = pgt_info->num_of_ptes;
161         if (!num_of_ptes_left)
162                 free_hop(ctx, hop_addr);
163
164         return num_of_ptes_left;
165 }
166
167 static inline u64 get_hopN_pte_addr(struct hl_ctx *ctx, u64 hop_addr,
168                                         u64 virt_addr, u64 mask, u64 shift)
169 {
170         return hop_addr + ctx->hdev->asic_prop.mmu_pte_size *
171                         ((virt_addr & mask) >> shift);
172 }
173
174 static inline u64 get_hop0_pte_addr(struct hl_ctx *ctx, u64 hop_addr, u64 vaddr)
175 {
176         return get_hopN_pte_addr(ctx, hop_addr, vaddr, HOP0_MASK, HOP0_SHIFT);
177 }
178
179 static inline u64 get_hop1_pte_addr(struct hl_ctx *ctx, u64 hop_addr, u64 vaddr)
180 {
181         return get_hopN_pte_addr(ctx, hop_addr, vaddr, HOP1_MASK, HOP1_SHIFT);
182 }
183
184 static inline u64 get_hop2_pte_addr(struct hl_ctx *ctx, u64 hop_addr, u64 vaddr)
185 {
186         return get_hopN_pte_addr(ctx, hop_addr, vaddr, HOP2_MASK, HOP2_SHIFT);
187 }
188
189 static inline u64 get_hop3_pte_addr(struct hl_ctx *ctx, u64 hop_addr, u64 vaddr)
190 {
191         return get_hopN_pte_addr(ctx, hop_addr, vaddr, HOP3_MASK, HOP3_SHIFT);
192 }
193
194 static inline u64 get_hop4_pte_addr(struct hl_ctx *ctx, u64 hop_addr, u64 vaddr)
195 {
196         return get_hopN_pte_addr(ctx, hop_addr, vaddr, HOP4_MASK, HOP4_SHIFT);
197 }
198
199 static inline u64 get_next_hop_addr(struct hl_ctx *ctx, u64 curr_pte)
200 {
201         if (curr_pte & PAGE_PRESENT_MASK)
202                 return curr_pte & PHYS_ADDR_MASK;
203         else
204                 return ULLONG_MAX;
205 }
206
207 static inline u64 get_alloc_next_hop_addr(struct hl_ctx *ctx, u64 curr_pte,
208                                                 bool *is_new_hop)
209 {
210         u64 hop_addr = get_next_hop_addr(ctx, curr_pte);
211
212         if (hop_addr == ULLONG_MAX) {
213                 hop_addr = alloc_hop(ctx);
214                 *is_new_hop = (hop_addr != ULLONG_MAX);
215         }
216
217         return hop_addr;
218 }
219
220 /* translates shadow address inside hop to a physical address */
221 static inline u64 get_phys_addr(struct hl_ctx *ctx, u64 shadow_addr)
222 {
223         u64 page_mask = (ctx->hdev->asic_prop.mmu_hop_table_size - 1);
224         u64 shadow_hop_addr = shadow_addr & ~page_mask;
225         u64 pte_offset = shadow_addr & page_mask;
226         u64 phys_hop_addr;
227
228         if (shadow_hop_addr != get_hop0_addr(ctx))
229                 phys_hop_addr = get_pgt_info(ctx, shadow_hop_addr)->phys_addr;
230         else
231                 phys_hop_addr = get_phys_hop0_addr(ctx);
232
233         return phys_hop_addr + pte_offset;
234 }
235
236 static int dram_default_mapping_init(struct hl_ctx *ctx)
237 {
238         struct hl_device *hdev = ctx->hdev;
239         struct asic_fixed_properties *prop = &hdev->asic_prop;
240         u64 num_of_hop3, total_hops, hop0_addr, hop1_addr, hop2_addr,
241                 hop2_pte_addr, hop3_pte_addr, pte_val;
242         int rc, i, j, hop3_allocated = 0;
243
244         if (!hdev->dram_supports_virtual_memory ||
245                         !hdev->dram_default_page_mapping)
246                 return 0;
247
248         num_of_hop3 = prop->dram_size_for_default_page_mapping;
249         do_div(num_of_hop3, prop->dram_page_size);
250         do_div(num_of_hop3, PTE_ENTRIES_IN_HOP);
251
252         /* add hop1 and hop2 */
253         total_hops = num_of_hop3 + 2;
254
255         ctx->dram_default_hops = kzalloc(HL_PTE_SIZE * total_hops,  GFP_KERNEL);
256         if (!ctx->dram_default_hops)
257                 return -ENOMEM;
258
259         hop0_addr = get_hop0_addr(ctx);
260
261         hop1_addr = alloc_hop(ctx);
262         if (hop1_addr == ULLONG_MAX) {
263                 dev_err(hdev->dev, "failed to alloc hop 1\n");
264                 rc = -ENOMEM;
265                 goto hop1_err;
266         }
267
268         ctx->dram_default_hops[total_hops - 1] = hop1_addr;
269
270         hop2_addr = alloc_hop(ctx);
271         if (hop2_addr == ULLONG_MAX) {
272                 dev_err(hdev->dev, "failed to alloc hop 2\n");
273                 rc = -ENOMEM;
274                 goto hop2_err;
275         }
276
277         ctx->dram_default_hops[total_hops - 2] = hop2_addr;
278
279         for (i = 0 ; i < num_of_hop3 ; i++) {
280                 ctx->dram_default_hops[i] = alloc_hop(ctx);
281                 if (ctx->dram_default_hops[i] == ULLONG_MAX) {
282                         dev_err(hdev->dev, "failed to alloc hop 3, i: %d\n", i);
283                         rc = -ENOMEM;
284                         goto hop3_err;
285                 }
286                 hop3_allocated++;
287         }
288
289         /* need only pte 0 in hops 0 and 1 */
290         pte_val = (hop1_addr & PTE_PHYS_ADDR_MASK) | PAGE_PRESENT_MASK;
291         write_pte(ctx, hop0_addr, pte_val);
292
293         pte_val = (hop2_addr & PTE_PHYS_ADDR_MASK) | PAGE_PRESENT_MASK;
294         write_pte(ctx, hop1_addr, pte_val);
295         get_pte(ctx, hop1_addr);
296
297         hop2_pte_addr = hop2_addr;
298         for (i = 0 ; i < num_of_hop3 ; i++) {
299                 pte_val = (ctx->dram_default_hops[i] & PTE_PHYS_ADDR_MASK) |
300                                 PAGE_PRESENT_MASK;
301                 write_pte(ctx, hop2_pte_addr, pte_val);
302                 get_pte(ctx, hop2_addr);
303                 hop2_pte_addr += HL_PTE_SIZE;
304         }
305
306         pte_val = (prop->mmu_dram_default_page_addr & PTE_PHYS_ADDR_MASK) |
307                         LAST_MASK | PAGE_PRESENT_MASK;
308
309         for (i = 0 ; i < num_of_hop3 ; i++) {
310                 hop3_pte_addr = ctx->dram_default_hops[i];
311                 for (j = 0 ; j < PTE_ENTRIES_IN_HOP ; j++) {
312                         write_final_pte(ctx, hop3_pte_addr, pte_val);
313                         get_pte(ctx, ctx->dram_default_hops[i]);
314                         hop3_pte_addr += HL_PTE_SIZE;
315                 }
316         }
317
318         flush(ctx);
319
320         return 0;
321
322 hop3_err:
323         for (i = 0 ; i < hop3_allocated ; i++)
324                 free_hop(ctx, ctx->dram_default_hops[i]);
325
326         free_hop(ctx, hop2_addr);
327 hop2_err:
328         free_hop(ctx, hop1_addr);
329 hop1_err:
330         kfree(ctx->dram_default_hops);
331
332         return rc;
333 }
334
335 static void dram_default_mapping_fini(struct hl_ctx *ctx)
336 {
337         struct hl_device *hdev = ctx->hdev;
338         struct asic_fixed_properties *prop = &hdev->asic_prop;
339         u64 num_of_hop3, total_hops, hop0_addr, hop1_addr, hop2_addr,
340                 hop2_pte_addr, hop3_pte_addr;
341         int i, j;
342
343         if (!hdev->dram_supports_virtual_memory ||
344                         !hdev->dram_default_page_mapping)
345                 return;
346
347         num_of_hop3 = prop->dram_size_for_default_page_mapping;
348         do_div(num_of_hop3, prop->dram_page_size);
349         do_div(num_of_hop3, PTE_ENTRIES_IN_HOP);
350
351         hop0_addr = get_hop0_addr(ctx);
352         /* add hop1 and hop2 */
353         total_hops = num_of_hop3 + 2;
354         hop1_addr = ctx->dram_default_hops[total_hops - 1];
355         hop2_addr = ctx->dram_default_hops[total_hops - 2];
356
357         for (i = 0 ; i < num_of_hop3 ; i++) {
358                 hop3_pte_addr = ctx->dram_default_hops[i];
359                 for (j = 0 ; j < PTE_ENTRIES_IN_HOP ; j++) {
360                         clear_pte(ctx, hop3_pte_addr);
361                         put_pte(ctx, ctx->dram_default_hops[i]);
362                         hop3_pte_addr += HL_PTE_SIZE;
363                 }
364         }
365
366         hop2_pte_addr = hop2_addr;
367         hop2_pte_addr = hop2_addr;
368         for (i = 0 ; i < num_of_hop3 ; i++) {
369                 clear_pte(ctx, hop2_pte_addr);
370                 put_pte(ctx, hop2_addr);
371                 hop2_pte_addr += HL_PTE_SIZE;
372         }
373
374         clear_pte(ctx, hop1_addr);
375         put_pte(ctx, hop1_addr);
376         clear_pte(ctx, hop0_addr);
377
378         kfree(ctx->dram_default_hops);
379
380         flush(ctx);
381 }
382
383 /**
384  * hl_mmu_init() - initialize the MMU module.
385  * @hdev: habanalabs device structure.
386  *
387  * This function does the following:
388  * - Allocate max_asid zeroed hop0 pgts so no mapping is available.
389  * - Enable MMU in H/W.
390  * - Invalidate the MMU cache.
391  * - Create a pool of pages for pgt_infos.
392  *
393  * This function depends on DMA QMAN to be working!
394  *
395  * Return: 0 for success, non-zero for failure.
396  */
397 int hl_mmu_init(struct hl_device *hdev)
398 {
399         struct asic_fixed_properties *prop = &hdev->asic_prop;
400         int rc;
401
402         if (!hdev->mmu_enable)
403                 return 0;
404
405         /* MMU H/W init was already done in device hw_init() */
406
407         mutex_init(&hdev->mmu_cache_lock);
408
409         hdev->mmu_pgt_pool =
410                         gen_pool_create(__ffs(prop->mmu_hop_table_size), -1);
411
412         if (!hdev->mmu_pgt_pool) {
413                 dev_err(hdev->dev, "Failed to create page gen pool\n");
414                 rc = -ENOMEM;
415                 goto err_pool_create;
416         }
417
418         rc = gen_pool_add(hdev->mmu_pgt_pool, prop->mmu_pgt_addr +
419                         prop->mmu_hop0_tables_total_size,
420                         prop->mmu_pgt_size - prop->mmu_hop0_tables_total_size,
421                         -1);
422         if (rc) {
423                 dev_err(hdev->dev, "Failed to add memory to page gen pool\n");
424                 goto err_pool_add;
425         }
426
427         hdev->mmu_shadow_hop0 = kvmalloc_array(prop->max_asid,
428                                         prop->mmu_hop_table_size,
429                                         GFP_KERNEL | __GFP_ZERO);
430         if (!hdev->mmu_shadow_hop0) {
431                 rc = -ENOMEM;
432                 goto err_pool_add;
433         }
434
435         return 0;
436
437 err_pool_add:
438         gen_pool_destroy(hdev->mmu_pgt_pool);
439 err_pool_create:
440         mutex_destroy(&hdev->mmu_cache_lock);
441
442         return rc;
443 }
444
445 /**
446  * hl_mmu_fini() - release the MMU module.
447  * @hdev: habanalabs device structure.
448  *
449  * This function does the following:
450  * - Disable MMU in H/W.
451  * - Free the pgt_infos pool.
452  *
453  * All contexts should be freed before calling this function.
454  */
455 void hl_mmu_fini(struct hl_device *hdev)
456 {
457         if (!hdev->mmu_enable)
458                 return;
459
460         kvfree(hdev->mmu_shadow_hop0);
461         gen_pool_destroy(hdev->mmu_pgt_pool);
462         mutex_destroy(&hdev->mmu_cache_lock);
463
464         /* MMU H/W fini will be done in device hw_fini() */
465 }
466
467 /**
468  * hl_mmu_ctx_init() - initialize a context for using the MMU module.
469  * @ctx: pointer to the context structure to initialize.
470  *
471  * Initialize a mutex to protect the concurrent mapping flow, a hash to hold all
472  * page tables hops related to this context.
473  * Return: 0 on success, non-zero otherwise.
474  */
475 int hl_mmu_ctx_init(struct hl_ctx *ctx)
476 {
477         struct hl_device *hdev = ctx->hdev;
478
479         if (!hdev->mmu_enable)
480                 return 0;
481
482         mutex_init(&ctx->mmu_lock);
483         hash_init(ctx->mmu_phys_hash);
484         hash_init(ctx->mmu_shadow_hash);
485
486         return dram_default_mapping_init(ctx);
487 }
488
489 /*
490  * hl_mmu_ctx_fini - disable a ctx from using the mmu module
491  *
492  * @ctx: pointer to the context structure
493  *
494  * This function does the following:
495  * - Free any pgts which were not freed yet
496  * - Free the mutex
497  * - Free DRAM default page mapping hops
498  */
499 void hl_mmu_ctx_fini(struct hl_ctx *ctx)
500 {
501         struct hl_device *hdev = ctx->hdev;
502         struct pgt_info *pgt_info;
503         struct hlist_node *tmp;
504         int i;
505
506         if (!hdev->mmu_enable)
507                 return;
508
509         dram_default_mapping_fini(ctx);
510
511         if (!hash_empty(ctx->mmu_shadow_hash))
512                 dev_err(hdev->dev, "ctx is freed while it has pgts in use\n");
513
514         hash_for_each_safe(ctx->mmu_shadow_hash, i, tmp, pgt_info, node) {
515                 dev_err(hdev->dev,
516                         "pgt_info of addr 0x%llx of asid %d was not destroyed, num_ptes: %d\n",
517                         pgt_info->phys_addr, ctx->asid, pgt_info->num_of_ptes);
518                 free_hop(ctx, pgt_info->shadow_addr);
519         }
520
521         mutex_destroy(&ctx->mmu_lock);
522 }
523
524 static int _hl_mmu_unmap(struct hl_ctx *ctx, u64 virt_addr)
525 {
526         struct hl_device *hdev = ctx->hdev;
527         struct asic_fixed_properties *prop = &hdev->asic_prop;
528         u64 hop0_addr = 0, hop0_pte_addr = 0,
529                 hop1_addr = 0, hop1_pte_addr = 0,
530                 hop2_addr = 0, hop2_pte_addr = 0,
531                 hop3_addr = 0, hop3_pte_addr = 0,
532                 hop4_addr = 0, hop4_pte_addr = 0,
533                 curr_pte;
534         bool is_dram_addr, is_huge, clear_hop3 = true;
535
536         is_dram_addr = hl_mem_area_inside_range(virt_addr, PAGE_SIZE_2MB,
537                                 prop->va_space_dram_start_address,
538                                 prop->va_space_dram_end_address);
539
540         hop0_addr = get_hop0_addr(ctx);
541         hop0_pte_addr = get_hop0_pte_addr(ctx, hop0_addr, virt_addr);
542
543         curr_pte = *(u64 *) (uintptr_t) hop0_pte_addr;
544
545         hop1_addr = get_next_hop_addr(ctx, curr_pte);
546
547         if (hop1_addr == ULLONG_MAX)
548                 goto not_mapped;
549
550         hop1_pte_addr = get_hop1_pte_addr(ctx, hop1_addr, virt_addr);
551
552         curr_pte = *(u64 *) (uintptr_t) hop1_pte_addr;
553
554         hop2_addr = get_next_hop_addr(ctx, curr_pte);
555
556         if (hop2_addr == ULLONG_MAX)
557                 goto not_mapped;
558
559         hop2_pte_addr = get_hop2_pte_addr(ctx, hop2_addr, virt_addr);
560
561         curr_pte = *(u64 *) (uintptr_t) hop2_pte_addr;
562
563         hop3_addr = get_next_hop_addr(ctx, curr_pte);
564
565         if (hop3_addr == ULLONG_MAX)
566                 goto not_mapped;
567
568         hop3_pte_addr = get_hop3_pte_addr(ctx, hop3_addr, virt_addr);
569
570         curr_pte = *(u64 *) (uintptr_t) hop3_pte_addr;
571
572         is_huge = curr_pte & LAST_MASK;
573
574         if (is_dram_addr && !is_huge) {
575                 dev_err(hdev->dev,
576                                 "DRAM unmapping should use huge pages only\n");
577                 return -EFAULT;
578         }
579
580         if (!is_huge) {
581                 hop4_addr = get_next_hop_addr(ctx, curr_pte);
582
583                 if (hop4_addr == ULLONG_MAX)
584                         goto not_mapped;
585
586                 hop4_pte_addr = get_hop4_pte_addr(ctx, hop4_addr, virt_addr);
587
588                 curr_pte = *(u64 *) (uintptr_t) hop4_pte_addr;
589
590                 clear_hop3 = false;
591         }
592
593         if (hdev->dram_default_page_mapping && is_dram_addr) {
594                 u64 default_pte = (prop->mmu_dram_default_page_addr &
595                                 PTE_PHYS_ADDR_MASK) | LAST_MASK |
596                                         PAGE_PRESENT_MASK;
597                 if (curr_pte == default_pte) {
598                         dev_err(hdev->dev,
599                                 "DRAM: hop3 PTE points to zero page, can't unmap, va: 0x%llx\n",
600                                         virt_addr);
601                         goto not_mapped;
602                 }
603
604                 if (!(curr_pte & PAGE_PRESENT_MASK)) {
605                         dev_err(hdev->dev,
606                                 "DRAM: hop3 PTE is cleared! can't unmap, va: 0x%llx\n",
607                                         virt_addr);
608                         goto not_mapped;
609                 }
610
611                 write_final_pte(ctx, hop3_pte_addr, default_pte);
612                 put_pte(ctx, hop3_addr);
613         } else {
614                 if (!(curr_pte & PAGE_PRESENT_MASK))
615                         goto not_mapped;
616
617                 if (hop4_addr)
618                         clear_pte(ctx, hop4_pte_addr);
619                 else
620                         clear_pte(ctx, hop3_pte_addr);
621
622                 if (hop4_addr && !put_pte(ctx, hop4_addr))
623                         clear_hop3 = true;
624
625                 if (!clear_hop3)
626                         goto flush;
627
628                 clear_pte(ctx, hop3_pte_addr);
629
630                 if (put_pte(ctx, hop3_addr))
631                         goto flush;
632
633                 clear_pte(ctx, hop2_pte_addr);
634
635                 if (put_pte(ctx, hop2_addr))
636                         goto flush;
637
638                 clear_pte(ctx, hop1_pte_addr);
639
640                 if (put_pte(ctx, hop1_addr))
641                         goto flush;
642
643                 clear_pte(ctx, hop0_pte_addr);
644         }
645
646 flush:
647         flush(ctx);
648
649         return 0;
650
651 not_mapped:
652         dev_err(hdev->dev, "virt addr 0x%llx is not mapped to phys addr\n",
653                 virt_addr);
654
655         return -EINVAL;
656 }
657
658 /*
659  * hl_mmu_unmap - unmaps a virtual addr
660  *
661  * @ctx: pointer to the context structure
662  * @virt_addr: virt addr to map from
663  * @page_size: size of the page to unmap
664  *
665  * This function does the following:
666  * - Check that the virt addr is mapped
667  * - Unmap the virt addr and frees pgts if possible
668  * - Returns 0 on success, -EINVAL if the given addr is not mapped
669  *
670  * Because this function changes the page tables in the device and because it
671  * changes the MMU hash, it must be protected by a lock.
672  * However, because it maps only a single page, the lock should be implemented
673  * in a higher level in order to protect the entire mapping of the memory area
674  */
675 int hl_mmu_unmap(struct hl_ctx *ctx, u64 virt_addr, u32 page_size)
676 {
677         struct hl_device *hdev = ctx->hdev;
678         u64 real_virt_addr;
679         u32 real_page_size, npages;
680         int i, rc;
681
682         if (!hdev->mmu_enable)
683                 return 0;
684
685         /*
686          * The H/W handles mapping of 4KB/2MB page. Hence if the host page size
687          * is bigger, we break it to sub-pages and unmap them separately.
688          */
689         if ((page_size % PAGE_SIZE_2MB) == 0) {
690                 real_page_size = PAGE_SIZE_2MB;
691         } else if ((page_size % PAGE_SIZE_4KB) == 0) {
692                 real_page_size = PAGE_SIZE_4KB;
693         } else {
694                 dev_err(hdev->dev,
695                         "page size of %u is not 4KB nor 2MB aligned, can't unmap\n",
696                                 page_size);
697
698                 return -EFAULT;
699         }
700
701         npages = page_size / real_page_size;
702         real_virt_addr = virt_addr;
703
704         for (i = 0 ; i < npages ; i++) {
705                 rc = _hl_mmu_unmap(ctx, real_virt_addr);
706                 if (rc)
707                         return rc;
708
709                 real_virt_addr += real_page_size;
710         }
711
712         return 0;
713 }
714
715 static int _hl_mmu_map(struct hl_ctx *ctx, u64 virt_addr, u64 phys_addr,
716                 u32 page_size)
717 {
718         struct hl_device *hdev = ctx->hdev;
719         struct asic_fixed_properties *prop = &hdev->asic_prop;
720         u64 hop0_addr = 0, hop0_pte_addr = 0,
721                 hop1_addr = 0, hop1_pte_addr = 0,
722                 hop2_addr = 0, hop2_pte_addr = 0,
723                 hop3_addr = 0, hop3_pte_addr = 0,
724                 hop4_addr = 0, hop4_pte_addr = 0,
725                 curr_pte = 0;
726         bool hop1_new = false, hop2_new = false, hop3_new = false,
727                 hop4_new = false, is_huge, is_dram_addr;
728         int rc = -ENOMEM;
729
730         /*
731          * This mapping function can map a 4KB/2MB page. For 2MB page there are
732          * only 3 hops rather than 4. Currently the DRAM allocation uses 2MB
733          * pages only but user memory could have been allocated with one of the
734          * two page sizes. Since this is a common code for all the three cases,
735          * we need this hugs page check.
736          */
737         is_huge = page_size == PAGE_SIZE_2MB;
738
739         is_dram_addr = hl_mem_area_inside_range(virt_addr, page_size,
740                                 prop->va_space_dram_start_address,
741                                 prop->va_space_dram_end_address);
742
743         if (is_dram_addr && !is_huge) {
744                 dev_err(hdev->dev, "DRAM mapping should use huge pages only\n");
745                 return -EFAULT;
746         }
747
748         hop0_addr = get_hop0_addr(ctx);
749         hop0_pte_addr = get_hop0_pte_addr(ctx, hop0_addr, virt_addr);
750         curr_pte = *(u64 *) (uintptr_t) hop0_pte_addr;
751
752         hop1_addr = get_alloc_next_hop_addr(ctx, curr_pte, &hop1_new);
753         if (hop1_addr == ULLONG_MAX)
754                 goto err;
755
756         hop1_pte_addr = get_hop1_pte_addr(ctx, hop1_addr, virt_addr);
757         curr_pte = *(u64 *) (uintptr_t) hop1_pte_addr;
758
759         hop2_addr = get_alloc_next_hop_addr(ctx, curr_pte, &hop2_new);
760         if (hop2_addr == ULLONG_MAX)
761                 goto err;
762
763         hop2_pte_addr = get_hop2_pte_addr(ctx, hop2_addr, virt_addr);
764         curr_pte = *(u64 *) (uintptr_t) hop2_pte_addr;
765
766         hop3_addr = get_alloc_next_hop_addr(ctx, curr_pte, &hop3_new);
767         if (hop3_addr == ULLONG_MAX)
768                 goto err;
769
770         hop3_pte_addr = get_hop3_pte_addr(ctx, hop3_addr, virt_addr);
771         curr_pte = *(u64 *) (uintptr_t) hop3_pte_addr;
772
773         if (!is_huge) {
774                 hop4_addr = get_alloc_next_hop_addr(ctx, curr_pte, &hop4_new);
775                 if (hop4_addr == ULLONG_MAX)
776                         goto err;
777
778                 hop4_pte_addr = get_hop4_pte_addr(ctx, hop4_addr, virt_addr);
779                 curr_pte = *(u64 *) (uintptr_t) hop4_pte_addr;
780         }
781
782         if (hdev->dram_default_page_mapping && is_dram_addr) {
783                 u64 default_pte = (prop->mmu_dram_default_page_addr &
784                                         PTE_PHYS_ADDR_MASK) | LAST_MASK |
785                                                 PAGE_PRESENT_MASK;
786
787                 if (curr_pte != default_pte) {
788                         dev_err(hdev->dev,
789                                 "DRAM: mapping already exists for virt_addr 0x%llx\n",
790                                         virt_addr);
791                         rc = -EINVAL;
792                         goto err;
793                 }
794
795                 if (hop1_new || hop2_new || hop3_new || hop4_new) {
796                         dev_err(hdev->dev,
797                                 "DRAM mapping should not allocate more hops\n");
798                         rc = -EFAULT;
799                         goto err;
800                 }
801         } else if (curr_pte & PAGE_PRESENT_MASK) {
802                 dev_err(hdev->dev,
803                         "mapping already exists for virt_addr 0x%llx\n",
804                                 virt_addr);
805
806                 dev_dbg(hdev->dev, "hop0 pte: 0x%llx (0x%llx)\n",
807                         *(u64 *) (uintptr_t) hop0_pte_addr, hop0_pte_addr);
808                 dev_dbg(hdev->dev, "hop1 pte: 0x%llx (0x%llx)\n",
809                         *(u64 *) (uintptr_t) hop1_pte_addr, hop1_pte_addr);
810                 dev_dbg(hdev->dev, "hop2 pte: 0x%llx (0x%llx)\n",
811                         *(u64 *) (uintptr_t) hop2_pte_addr, hop2_pte_addr);
812                 dev_dbg(hdev->dev, "hop3 pte: 0x%llx (0x%llx)\n",
813                         *(u64 *) (uintptr_t) hop3_pte_addr, hop3_pte_addr);
814
815                 if (!is_huge)
816                         dev_dbg(hdev->dev, "hop4 pte: 0x%llx (0x%llx)\n",
817                                 *(u64 *) (uintptr_t) hop4_pte_addr,
818                                 hop4_pte_addr);
819
820                 rc = -EINVAL;
821                 goto err;
822         }
823
824         curr_pte = (phys_addr & PTE_PHYS_ADDR_MASK) | LAST_MASK
825                         | PAGE_PRESENT_MASK;
826
827         if (is_huge)
828                 write_final_pte(ctx, hop3_pte_addr, curr_pte);
829         else
830                 write_final_pte(ctx, hop4_pte_addr, curr_pte);
831
832         if (hop1_new) {
833                 curr_pte =
834                         (hop1_addr & PTE_PHYS_ADDR_MASK) | PAGE_PRESENT_MASK;
835                 write_pte(ctx, hop0_pte_addr, curr_pte);
836         }
837         if (hop2_new) {
838                 curr_pte =
839                         (hop2_addr & PTE_PHYS_ADDR_MASK) | PAGE_PRESENT_MASK;
840                 write_pte(ctx, hop1_pte_addr, curr_pte);
841                 get_pte(ctx, hop1_addr);
842         }
843         if (hop3_new) {
844                 curr_pte =
845                         (hop3_addr & PTE_PHYS_ADDR_MASK) | PAGE_PRESENT_MASK;
846                 write_pte(ctx, hop2_pte_addr, curr_pte);
847                 get_pte(ctx, hop2_addr);
848         }
849
850         if (!is_huge) {
851                 if (hop4_new) {
852                         curr_pte = (hop4_addr & PTE_PHYS_ADDR_MASK) |
853                                         PAGE_PRESENT_MASK;
854                         write_pte(ctx, hop3_pte_addr, curr_pte);
855                         get_pte(ctx, hop3_addr);
856                 }
857
858                 get_pte(ctx, hop4_addr);
859         } else {
860                 get_pte(ctx, hop3_addr);
861         }
862
863         flush(ctx);
864
865         return 0;
866
867 err:
868         if (hop4_new)
869                 free_hop(ctx, hop4_addr);
870         if (hop3_new)
871                 free_hop(ctx, hop3_addr);
872         if (hop2_new)
873                 free_hop(ctx, hop2_addr);
874         if (hop1_new)
875                 free_hop(ctx, hop1_addr);
876
877         return rc;
878 }
879
880 /*
881  * hl_mmu_map - maps a virtual addr to physical addr
882  *
883  * @ctx: pointer to the context structure
884  * @virt_addr: virt addr to map from
885  * @phys_addr: phys addr to map to
886  * @page_size: physical page size
887  *
888  * This function does the following:
889  * - Check that the virt addr is not mapped
890  * - Allocate pgts as necessary in order to map the virt addr to the phys
891  * - Returns 0 on success, -EINVAL if addr is already mapped, or -ENOMEM.
892  *
893  * Because this function changes the page tables in the device and because it
894  * changes the MMU hash, it must be protected by a lock.
895  * However, because it maps only a single page, the lock should be implemented
896  * in a higher level in order to protect the entire mapping of the memory area
897  */
898 int hl_mmu_map(struct hl_ctx *ctx, u64 virt_addr, u64 phys_addr, u32 page_size)
899 {
900         struct hl_device *hdev = ctx->hdev;
901         u64 real_virt_addr, real_phys_addr;
902         u32 real_page_size, npages;
903         int i, rc, mapped_cnt = 0;
904
905         if (!hdev->mmu_enable)
906                 return 0;
907
908         /*
909          * The H/W handles mapping of 4KB/2MB page. Hence if the host page size
910          * is bigger, we break it to sub-pages and map them separately.
911          */
912         if ((page_size % PAGE_SIZE_2MB) == 0) {
913                 real_page_size = PAGE_SIZE_2MB;
914         } else if ((page_size % PAGE_SIZE_4KB) == 0) {
915                 real_page_size = PAGE_SIZE_4KB;
916         } else {
917                 dev_err(hdev->dev,
918                         "page size of %u is not 4KB nor 2MB aligned, can't map\n",
919                                 page_size);
920
921                 return -EFAULT;
922         }
923
924         npages = page_size / real_page_size;
925         real_virt_addr = virt_addr;
926         real_phys_addr = phys_addr;
927
928         for (i = 0 ; i < npages ; i++) {
929                 rc = _hl_mmu_map(ctx, real_virt_addr, real_phys_addr,
930                                 real_page_size);
931                 if (rc)
932                         goto err;
933
934                 real_virt_addr += real_page_size;
935                 real_phys_addr += real_page_size;
936                 mapped_cnt++;
937         }
938
939         return 0;
940
941 err:
942         real_virt_addr = virt_addr;
943         for (i = 0 ; i < mapped_cnt ; i++) {
944                 if (_hl_mmu_unmap(ctx, real_virt_addr))
945                         dev_warn_ratelimited(hdev->dev,
946                                 "failed to unmap va: 0x%llx\n", real_virt_addr);
947
948                 real_virt_addr += real_page_size;
949         }
950
951         return rc;
952 }
953
954 /*
955  * hl_mmu_swap_out - marks all mapping of the given ctx as swapped out
956  *
957  * @ctx: pointer to the context structure
958  *
959  */
960 void hl_mmu_swap_out(struct hl_ctx *ctx)
961 {
962
963 }
964
965 /*
966  * hl_mmu_swap_in - marks all mapping of the given ctx as swapped in
967  *
968  * @ctx: pointer to the context structure
969  *
970  */
971 void hl_mmu_swap_in(struct hl_ctx *ctx)
972 {
973
974 }