| 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * Copyright (C) 2017-2018 Etnaviv Project |
| 4 | */ |
| 5 | |
| 6 | #include <linux/dma-mapping.h> |
| 7 | |
| 8 | #include "etnaviv_cmdbuf.h" |
| 9 | #include "etnaviv_gem.h" |
| 10 | #include "etnaviv_gpu.h" |
| 11 | #include "etnaviv_mmu.h" |
| 12 | |
| 13 | #define SUBALLOC_SIZE SZ_512K |
| 14 | #define SUBALLOC_GRANULE SZ_4K |
| 15 | #define SUBALLOC_GRANULES (SUBALLOC_SIZE / SUBALLOC_GRANULE) |
| 16 | |
| 17 | struct etnaviv_cmdbuf_suballoc { |
| 18 | /* suballocated dma buffer properties */ |
| 19 | struct device *dev; |
| 20 | void *vaddr; |
| 21 | dma_addr_t paddr; |
| 22 | |
| 23 | /* allocation management */ |
| 24 | struct mutex lock; |
| 25 | DECLARE_BITMAP(granule_map, SUBALLOC_GRANULES); |
| 26 | int free_space; |
| 27 | wait_queue_head_t free_event; |
| 28 | }; |
| 29 | |
| 30 | struct etnaviv_cmdbuf_suballoc * |
| 31 | etnaviv_cmdbuf_suballoc_new(struct device *dev) |
| 32 | { |
| 33 | struct etnaviv_cmdbuf_suballoc *suballoc; |
| 34 | int ret; |
| 35 | |
| 36 | suballoc = kzalloc(sizeof(*suballoc), GFP_KERNEL); |
| 37 | if (!suballoc) |
| 38 | return ERR_PTR(error: -ENOMEM); |
| 39 | |
| 40 | suballoc->dev = dev; |
| 41 | mutex_init(&suballoc->lock); |
| 42 | init_waitqueue_head(&suballoc->free_event); |
| 43 | |
| 44 | BUILD_BUG_ON(ETNAVIV_SOFTPIN_START_ADDRESS < SUBALLOC_SIZE); |
| 45 | suballoc->vaddr = dma_alloc_wc(dev, SUBALLOC_SIZE, |
| 46 | dma_addr: &suballoc->paddr, GFP_KERNEL); |
| 47 | if (!suballoc->vaddr) { |
| 48 | ret = -ENOMEM; |
| 49 | goto free_suballoc; |
| 50 | } |
| 51 | |
| 52 | return suballoc; |
| 53 | |
| 54 | free_suballoc: |
| 55 | mutex_destroy(lock: &suballoc->lock); |
| 56 | kfree(objp: suballoc); |
| 57 | |
| 58 | return ERR_PTR(error: ret); |
| 59 | } |
| 60 | |
| 61 | int etnaviv_cmdbuf_suballoc_map(struct etnaviv_cmdbuf_suballoc *suballoc, |
| 62 | struct etnaviv_iommu_context *context, |
| 63 | struct etnaviv_vram_mapping *mapping, |
| 64 | u32 memory_base) |
| 65 | { |
| 66 | return etnaviv_iommu_get_suballoc_va(ctx: context, mapping, memory_base, |
| 67 | paddr: suballoc->paddr, SUBALLOC_SIZE); |
| 68 | } |
| 69 | |
| 70 | void etnaviv_cmdbuf_suballoc_unmap(struct etnaviv_iommu_context *context, |
| 71 | struct etnaviv_vram_mapping *mapping) |
| 72 | { |
| 73 | etnaviv_iommu_put_suballoc_va(ctx: context, mapping); |
| 74 | } |
| 75 | |
| 76 | void etnaviv_cmdbuf_suballoc_destroy(struct etnaviv_cmdbuf_suballoc *suballoc) |
| 77 | { |
| 78 | dma_free_wc(dev: suballoc->dev, SUBALLOC_SIZE, cpu_addr: suballoc->vaddr, |
| 79 | dma_addr: suballoc->paddr); |
| 80 | mutex_destroy(lock: &suballoc->lock); |
| 81 | kfree(objp: suballoc); |
| 82 | } |
| 83 | |
| 84 | int etnaviv_cmdbuf_init(struct etnaviv_cmdbuf_suballoc *suballoc, |
| 85 | struct etnaviv_cmdbuf *cmdbuf, u32 size) |
| 86 | { |
| 87 | int granule_offs, order, ret; |
| 88 | |
| 89 | cmdbuf->suballoc = suballoc; |
| 90 | cmdbuf->size = size; |
| 91 | |
| 92 | order = order_base_2(ALIGN(size, SUBALLOC_GRANULE) / SUBALLOC_GRANULE); |
| 93 | retry: |
| 94 | mutex_lock(&suballoc->lock); |
| 95 | granule_offs = bitmap_find_free_region(bitmap: suballoc->granule_map, |
| 96 | SUBALLOC_GRANULES, order); |
| 97 | if (granule_offs < 0) { |
| 98 | suballoc->free_space = 0; |
| 99 | mutex_unlock(lock: &suballoc->lock); |
| 100 | ret = wait_event_interruptible_timeout(suballoc->free_event, |
| 101 | suballoc->free_space, |
| 102 | secs_to_jiffies(10)); |
| 103 | if (!ret) { |
| 104 | dev_err(suballoc->dev, |
| 105 | "Timeout waiting for cmdbuf space\n" ); |
| 106 | return -ETIMEDOUT; |
| 107 | } |
| 108 | goto retry; |
| 109 | } |
| 110 | mutex_unlock(lock: &suballoc->lock); |
| 111 | cmdbuf->suballoc_offset = granule_offs * SUBALLOC_GRANULE; |
| 112 | cmdbuf->vaddr = suballoc->vaddr + cmdbuf->suballoc_offset; |
| 113 | |
| 114 | return 0; |
| 115 | } |
| 116 | |
| 117 | void etnaviv_cmdbuf_free(struct etnaviv_cmdbuf *cmdbuf) |
| 118 | { |
| 119 | struct etnaviv_cmdbuf_suballoc *suballoc = cmdbuf->suballoc; |
| 120 | int order = order_base_2(ALIGN(cmdbuf->size, SUBALLOC_GRANULE) / |
| 121 | SUBALLOC_GRANULE); |
| 122 | |
| 123 | if (!suballoc) |
| 124 | return; |
| 125 | |
| 126 | mutex_lock(&suballoc->lock); |
| 127 | bitmap_release_region(bitmap: suballoc->granule_map, |
| 128 | pos: cmdbuf->suballoc_offset / SUBALLOC_GRANULE, |
| 129 | order); |
| 130 | suballoc->free_space = 1; |
| 131 | mutex_unlock(lock: &suballoc->lock); |
| 132 | wake_up_all(&suballoc->free_event); |
| 133 | } |
| 134 | |
| 135 | u32 etnaviv_cmdbuf_get_va(struct etnaviv_cmdbuf *buf, |
| 136 | struct etnaviv_vram_mapping *mapping) |
| 137 | { |
| 138 | return mapping->iova + buf->suballoc_offset; |
| 139 | } |
| 140 | |
| 141 | dma_addr_t etnaviv_cmdbuf_get_pa(struct etnaviv_cmdbuf *buf) |
| 142 | { |
| 143 | return buf->suballoc->paddr + buf->suballoc_offset; |
| 144 | } |
| 145 | |