Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Copyright (C) 2017-2018 Etnaviv Project
0004  */
0005 
0006 #include <linux/dma-mapping.h>
0007 
0008 #include <drm/drm_mm.h>
0009 
0010 #include "etnaviv_cmdbuf.h"
0011 #include "etnaviv_gem.h"
0012 #include "etnaviv_gpu.h"
0013 #include "etnaviv_mmu.h"
0014 #include "etnaviv_perfmon.h"
0015 
0016 #define SUBALLOC_SIZE       SZ_512K
0017 #define SUBALLOC_GRANULE    SZ_4K
0018 #define SUBALLOC_GRANULES   (SUBALLOC_SIZE / SUBALLOC_GRANULE)
0019 
0020 struct etnaviv_cmdbuf_suballoc {
0021     /* suballocated dma buffer properties */
0022     struct device *dev;
0023     void *vaddr;
0024     dma_addr_t paddr;
0025 
0026     /* allocation management */
0027     struct mutex lock;
0028     DECLARE_BITMAP(granule_map, SUBALLOC_GRANULES);
0029     int free_space;
0030     wait_queue_head_t free_event;
0031 };
0032 
0033 struct etnaviv_cmdbuf_suballoc *
0034 etnaviv_cmdbuf_suballoc_new(struct device *dev)
0035 {
0036     struct etnaviv_cmdbuf_suballoc *suballoc;
0037     int ret;
0038 
0039     suballoc = kzalloc(sizeof(*suballoc), GFP_KERNEL);
0040     if (!suballoc)
0041         return ERR_PTR(-ENOMEM);
0042 
0043     suballoc->dev = dev;
0044     mutex_init(&suballoc->lock);
0045     init_waitqueue_head(&suballoc->free_event);
0046 
0047     BUILD_BUG_ON(ETNAVIV_SOFTPIN_START_ADDRESS < SUBALLOC_SIZE);
0048     suballoc->vaddr = dma_alloc_wc(dev, SUBALLOC_SIZE,
0049                        &suballoc->paddr, GFP_KERNEL);
0050     if (!suballoc->vaddr) {
0051         ret = -ENOMEM;
0052         goto free_suballoc;
0053     }
0054 
0055     return suballoc;
0056 
0057 free_suballoc:
0058     kfree(suballoc);
0059 
0060     return ERR_PTR(ret);
0061 }
0062 
0063 int etnaviv_cmdbuf_suballoc_map(struct etnaviv_cmdbuf_suballoc *suballoc,
0064                 struct etnaviv_iommu_context *context,
0065                 struct etnaviv_vram_mapping *mapping,
0066                 u32 memory_base)
0067 {
0068     return etnaviv_iommu_get_suballoc_va(context, mapping, memory_base,
0069                          suballoc->paddr, SUBALLOC_SIZE);
0070 }
0071 
0072 void etnaviv_cmdbuf_suballoc_unmap(struct etnaviv_iommu_context *context,
0073                    struct etnaviv_vram_mapping *mapping)
0074 {
0075     etnaviv_iommu_put_suballoc_va(context, mapping);
0076 }
0077 
0078 void etnaviv_cmdbuf_suballoc_destroy(struct etnaviv_cmdbuf_suballoc *suballoc)
0079 {
0080     dma_free_wc(suballoc->dev, SUBALLOC_SIZE, suballoc->vaddr,
0081             suballoc->paddr);
0082     kfree(suballoc);
0083 }
0084 
0085 int etnaviv_cmdbuf_init(struct etnaviv_cmdbuf_suballoc *suballoc,
0086             struct etnaviv_cmdbuf *cmdbuf, u32 size)
0087 {
0088     int granule_offs, order, ret;
0089 
0090     cmdbuf->suballoc = suballoc;
0091     cmdbuf->size = size;
0092 
0093     order = order_base_2(ALIGN(size, SUBALLOC_GRANULE) / SUBALLOC_GRANULE);
0094 retry:
0095     mutex_lock(&suballoc->lock);
0096     granule_offs = bitmap_find_free_region(suballoc->granule_map,
0097                     SUBALLOC_GRANULES, order);
0098     if (granule_offs < 0) {
0099         suballoc->free_space = 0;
0100         mutex_unlock(&suballoc->lock);
0101         ret = wait_event_interruptible_timeout(suballoc->free_event,
0102                                suballoc->free_space,
0103                                msecs_to_jiffies(10 * 1000));
0104         if (!ret) {
0105             dev_err(suballoc->dev,
0106                 "Timeout waiting for cmdbuf space\n");
0107             return -ETIMEDOUT;
0108         }
0109         goto retry;
0110     }
0111     mutex_unlock(&suballoc->lock);
0112     cmdbuf->suballoc_offset = granule_offs * SUBALLOC_GRANULE;
0113     cmdbuf->vaddr = suballoc->vaddr + cmdbuf->suballoc_offset;
0114 
0115     return 0;
0116 }
0117 
0118 void etnaviv_cmdbuf_free(struct etnaviv_cmdbuf *cmdbuf)
0119 {
0120     struct etnaviv_cmdbuf_suballoc *suballoc = cmdbuf->suballoc;
0121     int order = order_base_2(ALIGN(cmdbuf->size, SUBALLOC_GRANULE) /
0122                  SUBALLOC_GRANULE);
0123 
0124     mutex_lock(&suballoc->lock);
0125     bitmap_release_region(suballoc->granule_map,
0126                   cmdbuf->suballoc_offset / SUBALLOC_GRANULE,
0127                   order);
0128     suballoc->free_space = 1;
0129     mutex_unlock(&suballoc->lock);
0130     wake_up_all(&suballoc->free_event);
0131 }
0132 
0133 u32 etnaviv_cmdbuf_get_va(struct etnaviv_cmdbuf *buf,
0134               struct etnaviv_vram_mapping *mapping)
0135 {
0136     return mapping->iova + buf->suballoc_offset;
0137 }
0138 
0139 dma_addr_t etnaviv_cmdbuf_get_pa(struct etnaviv_cmdbuf *buf)
0140 {
0141     return buf->suballoc->paddr + buf->suballoc_offset;
0142 }