Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 1 | /* |
Rob Clark | 8bb0daf | 2013-02-11 12:43:09 -0500 | [diff] [blame] | 2 | * drivers/gpu/drm/omapdrm/omap_gem.c |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2011 Texas Instruments |
| 5 | * Author: Rob Clark <rob.clark@linaro.org> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify it |
| 8 | * under the terms of the GNU General Public License version 2 as published by |
| 9 | * the Free Software Foundation. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, but WITHOUT |
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 14 | * more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along with |
| 17 | * this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | */ |
| 19 | |
Arnd Bergmann | 2d80245 | 2016-05-11 18:01:45 +0200 | [diff] [blame] | 20 | #include <linux/seq_file.h> |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 21 | #include <linux/shmem_fs.h> |
Laurent Pinchart | 2d278f5 | 2015-03-05 21:31:37 +0200 | [diff] [blame] | 22 | #include <linux/spinlock.h> |
Dan Williams | 01c8f1c | 2016-01-15 16:56:40 -0800 | [diff] [blame] | 23 | #include <linux/pfn_t.h> |
Laurent Pinchart | 2d278f5 | 2015-03-05 21:31:37 +0200 | [diff] [blame] | 24 | |
David Herrmann | 0de2397 | 2013-07-24 21:07:52 +0200 | [diff] [blame] | 25 | #include <drm/drm_vma_manager.h> |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 26 | |
| 27 | #include "omap_drv.h" |
Rob Clark | f7f9f45 | 2011-12-05 19:19:22 -0600 | [diff] [blame] | 28 | #include "omap_dmm_tiler.h" |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 29 | |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 30 | /* |
| 31 | * GEM buffer object implementation. |
| 32 | */ |
| 33 | |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 34 | /* note: we use upper 8 bits of flags for driver-internal flags: */ |
Laurent Pinchart | cdb0381 | 2015-12-14 22:39:37 +0200 | [diff] [blame] | 35 | #define OMAP_BO_MEM_DMA_API 0x01000000 /* memory allocated with the dma_alloc_* API */ |
| 36 | #define OMAP_BO_MEM_SHMEM 0x02000000 /* memory allocated through shmem backing */ |
Laurent Pinchart | b22e669 | 2015-12-14 22:39:44 +0200 | [diff] [blame] | 37 | #define OMAP_BO_MEM_DMABUF 0x08000000 /* memory imported from a dmabuf */ |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 38 | |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 39 | struct omap_gem_object { |
| 40 | struct drm_gem_object base; |
| 41 | |
Rob Clark | f6b6036 | 2012-03-05 10:48:36 -0600 | [diff] [blame] | 42 | struct list_head mm_list; |
| 43 | |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 44 | uint32_t flags; |
| 45 | |
Rob Clark | f7f9f45 | 2011-12-05 19:19:22 -0600 | [diff] [blame] | 46 | /** width/height for tiled formats (rounded up to slot boundaries) */ |
| 47 | uint16_t width, height; |
| 48 | |
Rob Clark | a6a9182 | 2011-12-09 23:26:08 -0600 | [diff] [blame] | 49 | /** roll applied when mapping to DMM */ |
| 50 | uint32_t roll; |
| 51 | |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 52 | /** |
Laurent Pinchart | b22e669 | 2015-12-14 22:39:44 +0200 | [diff] [blame] | 53 | * paddr contains the buffer DMA address. It is valid for |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 54 | * |
Laurent Pinchart | b22e669 | 2015-12-14 22:39:44 +0200 | [diff] [blame] | 55 | * - buffers allocated through the DMA mapping API (with the |
| 56 | * OMAP_BO_MEM_DMA_API flag set) |
| 57 | * |
| 58 | * - buffers imported from dmabuf (with the OMAP_BO_MEM_DMABUF flag set) |
| 59 | * if they are physically contiguous (when sgt->orig_nents == 1) |
| 60 | * |
| 61 | * - buffers mapped through the TILER when paddr_cnt is not zero, in |
| 62 | * which case the DMA address points to the TILER aperture |
| 63 | * |
| 64 | * Physically contiguous buffers have their DMA address equal to the |
| 65 | * physical address as we don't remap those buffers through the TILER. |
| 66 | * |
| 67 | * Buffers mapped to the TILER have their DMA address pointing to the |
| 68 | * TILER aperture. As TILER mappings are refcounted (through paddr_cnt) |
| 69 | * the DMA address must be accessed through omap_get_get_paddr() to |
| 70 | * ensure that the mapping won't disappear unexpectedly. References must |
| 71 | * be released with omap_gem_put_paddr(). |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 72 | */ |
| 73 | dma_addr_t paddr; |
| 74 | |
| 75 | /** |
Rob Clark | f7f9f45 | 2011-12-05 19:19:22 -0600 | [diff] [blame] | 76 | * # of users of paddr |
| 77 | */ |
| 78 | uint32_t paddr_cnt; |
| 79 | |
| 80 | /** |
Laurent Pinchart | b22e669 | 2015-12-14 22:39:44 +0200 | [diff] [blame] | 81 | * If the buffer has been imported from a dmabuf the OMAP_DB_DMABUF flag |
| 82 | * is set and the sgt field is valid. |
| 83 | */ |
| 84 | struct sg_table *sgt; |
| 85 | |
| 86 | /** |
Rob Clark | f7f9f45 | 2011-12-05 19:19:22 -0600 | [diff] [blame] | 87 | * tiler block used when buffer is remapped in DMM/TILER. |
| 88 | */ |
| 89 | struct tiler_block *block; |
| 90 | |
| 91 | /** |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 92 | * Array of backing pages, if allocated. Note that pages are never |
| 93 | * allocated for buffers originally allocated from contiguous memory |
| 94 | */ |
| 95 | struct page **pages; |
| 96 | |
Rob Clark | f3bc9d2 | 2011-12-20 16:54:28 -0600 | [diff] [blame] | 97 | /** addresses corresponding to pages in above array */ |
| 98 | dma_addr_t *addrs; |
| 99 | |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 100 | /** |
| 101 | * Virtual address, if mapped. |
| 102 | */ |
| 103 | void *vaddr; |
| 104 | |
| 105 | /** |
| 106 | * sync-object allocated on demand (if needed) |
| 107 | * |
| 108 | * Per-buffer sync-object for tracking pending and completed hw/dma |
Tomi Valkeinen | 3f50eff | 2016-01-27 10:58:43 +0200 | [diff] [blame] | 109 | * read and write operations. |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 110 | */ |
| 111 | struct { |
| 112 | uint32_t write_pending; |
| 113 | uint32_t write_complete; |
| 114 | uint32_t read_pending; |
| 115 | uint32_t read_complete; |
| 116 | } *sync; |
| 117 | }; |
| 118 | |
Laurent Pinchart | 7ef93b0 | 2015-12-14 22:39:33 +0200 | [diff] [blame] | 119 | #define to_omap_bo(x) container_of(x, struct omap_gem_object, base) |
Rob Clark | c5b1247 | 2012-01-18 18:33:02 -0600 | [diff] [blame] | 120 | |
Rob Clark | f7f9f45 | 2011-12-05 19:19:22 -0600 | [diff] [blame] | 121 | /* To deal with userspace mmap'ings of 2d tiled buffers, which (a) are |
| 122 | * not necessarily pinned in TILER all the time, and (b) when they are |
| 123 | * they are not necessarily page aligned, we reserve one or more small |
| 124 | * regions in each of the 2d containers to use as a user-GART where we |
| 125 | * can create a second page-aligned mapping of parts of the buffer |
| 126 | * being accessed from userspace. |
| 127 | * |
| 128 | * Note that we could optimize slightly when we know that multiple |
| 129 | * tiler containers are backed by the same PAT.. but I'll leave that |
| 130 | * for later.. |
| 131 | */ |
| 132 | #define NUM_USERGART_ENTRIES 2 |
Laurent Pinchart | f430274 | 2015-12-14 22:39:34 +0200 | [diff] [blame] | 133 | struct omap_drm_usergart_entry { |
Rob Clark | f7f9f45 | 2011-12-05 19:19:22 -0600 | [diff] [blame] | 134 | struct tiler_block *block; /* the reserved tiler block */ |
| 135 | dma_addr_t paddr; |
| 136 | struct drm_gem_object *obj; /* the current pinned obj */ |
| 137 | pgoff_t obj_pgoff; /* page offset of obj currently |
| 138 | mapped in */ |
| 139 | }; |
Laurent Pinchart | f430274 | 2015-12-14 22:39:34 +0200 | [diff] [blame] | 140 | |
| 141 | struct omap_drm_usergart { |
| 142 | struct omap_drm_usergart_entry entry[NUM_USERGART_ENTRIES]; |
Rob Clark | f7f9f45 | 2011-12-05 19:19:22 -0600 | [diff] [blame] | 143 | int height; /* height in rows */ |
| 144 | int height_shift; /* ilog2(height in rows) */ |
| 145 | int slot_shift; /* ilog2(width per slot) */ |
| 146 | int stride_pfn; /* stride in pages */ |
| 147 | int last; /* index of last used entry */ |
Laurent Pinchart | f430274 | 2015-12-14 22:39:34 +0200 | [diff] [blame] | 148 | }; |
Rob Clark | f7f9f45 | 2011-12-05 19:19:22 -0600 | [diff] [blame] | 149 | |
Laurent Pinchart | b902f8f | 2015-12-14 22:39:32 +0200 | [diff] [blame] | 150 | /* ----------------------------------------------------------------------------- |
| 151 | * Helpers |
| 152 | */ |
| 153 | |
| 154 | /** get mmap offset */ |
| 155 | static uint64_t mmap_offset(struct drm_gem_object *obj) |
| 156 | { |
| 157 | struct drm_device *dev = obj->dev; |
| 158 | int ret; |
| 159 | size_t size; |
| 160 | |
| 161 | WARN_ON(!mutex_is_locked(&dev->struct_mutex)); |
| 162 | |
| 163 | /* Make it mmapable */ |
| 164 | size = omap_gem_mmap_size(obj); |
| 165 | ret = drm_gem_create_mmap_offset_size(obj, size); |
| 166 | if (ret) { |
| 167 | dev_err(dev->dev, "could not allocate mmap offset\n"); |
| 168 | return 0; |
| 169 | } |
| 170 | |
| 171 | return drm_vma_node_offset_addr(&obj->vma_node); |
| 172 | } |
| 173 | |
Laurent Pinchart | b22e669 | 2015-12-14 22:39:44 +0200 | [diff] [blame] | 174 | static bool is_contiguous(struct omap_gem_object *omap_obj) |
Laurent Pinchart | 7ef93b0 | 2015-12-14 22:39:33 +0200 | [diff] [blame] | 175 | { |
Laurent Pinchart | b22e669 | 2015-12-14 22:39:44 +0200 | [diff] [blame] | 176 | if (omap_obj->flags & OMAP_BO_MEM_DMA_API) |
| 177 | return true; |
| 178 | |
| 179 | if ((omap_obj->flags & OMAP_BO_MEM_DMABUF) && omap_obj->sgt->nents == 1) |
| 180 | return true; |
| 181 | |
| 182 | return false; |
Laurent Pinchart | 7ef93b0 | 2015-12-14 22:39:33 +0200 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | /* ----------------------------------------------------------------------------- |
| 186 | * Eviction |
| 187 | */ |
Rob Clark | f7f9f45 | 2011-12-05 19:19:22 -0600 | [diff] [blame] | 188 | |
| 189 | static void evict_entry(struct drm_gem_object *obj, |
Laurent Pinchart | f430274 | 2015-12-14 22:39:34 +0200 | [diff] [blame] | 190 | enum tiler_fmt fmt, struct omap_drm_usergart_entry *entry) |
Rob Clark | f7f9f45 | 2011-12-05 19:19:22 -0600 | [diff] [blame] | 191 | { |
David Herrmann | 6796cb1 | 2014-01-03 14:24:19 +0100 | [diff] [blame] | 192 | struct omap_gem_object *omap_obj = to_omap_bo(obj); |
Laurent Pinchart | f430274 | 2015-12-14 22:39:34 +0200 | [diff] [blame] | 193 | struct omap_drm_private *priv = obj->dev->dev_private; |
| 194 | int n = priv->usergart[fmt].height; |
David Herrmann | 6796cb1 | 2014-01-03 14:24:19 +0100 | [diff] [blame] | 195 | size_t size = PAGE_SIZE * n; |
| 196 | loff_t off = mmap_offset(obj) + |
| 197 | (entry->obj_pgoff << PAGE_SHIFT); |
| 198 | const int m = 1 + ((omap_obj->width << fmt) / PAGE_SIZE); |
| 199 | |
| 200 | if (m > 1) { |
| 201 | int i; |
| 202 | /* if stride > than PAGE_SIZE then sparse mapping: */ |
| 203 | for (i = n; i > 0; i--) { |
| 204 | unmap_mapping_range(obj->dev->anon_inode->i_mapping, |
| 205 | off, PAGE_SIZE, 1); |
| 206 | off += PAGE_SIZE * m; |
Rob Clark | e559895 | 2012-03-05 10:48:40 -0600 | [diff] [blame] | 207 | } |
David Herrmann | 6796cb1 | 2014-01-03 14:24:19 +0100 | [diff] [blame] | 208 | } else { |
| 209 | unmap_mapping_range(obj->dev->anon_inode->i_mapping, |
| 210 | off, size, 1); |
Rob Clark | f7f9f45 | 2011-12-05 19:19:22 -0600 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | entry->obj = NULL; |
| 214 | } |
| 215 | |
| 216 | /* Evict a buffer from usergart, if it is mapped there */ |
| 217 | static void evict(struct drm_gem_object *obj) |
| 218 | { |
| 219 | struct omap_gem_object *omap_obj = to_omap_bo(obj); |
Laurent Pinchart | f430274 | 2015-12-14 22:39:34 +0200 | [diff] [blame] | 220 | struct omap_drm_private *priv = obj->dev->dev_private; |
Rob Clark | f7f9f45 | 2011-12-05 19:19:22 -0600 | [diff] [blame] | 221 | |
| 222 | if (omap_obj->flags & OMAP_BO_TILED) { |
| 223 | enum tiler_fmt fmt = gem2fmt(omap_obj->flags); |
| 224 | int i; |
| 225 | |
Rob Clark | f7f9f45 | 2011-12-05 19:19:22 -0600 | [diff] [blame] | 226 | for (i = 0; i < NUM_USERGART_ENTRIES; i++) { |
Laurent Pinchart | f430274 | 2015-12-14 22:39:34 +0200 | [diff] [blame] | 227 | struct omap_drm_usergart_entry *entry = |
| 228 | &priv->usergart[fmt].entry[i]; |
| 229 | |
Rob Clark | f7f9f45 | 2011-12-05 19:19:22 -0600 | [diff] [blame] | 230 | if (entry->obj == obj) |
| 231 | evict_entry(obj, fmt, entry); |
| 232 | } |
| 233 | } |
| 234 | } |
| 235 | |
Laurent Pinchart | 7ef93b0 | 2015-12-14 22:39:33 +0200 | [diff] [blame] | 236 | /* ----------------------------------------------------------------------------- |
| 237 | * Page Management |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 238 | */ |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 239 | |
| 240 | /** ensure backing pages are allocated */ |
| 241 | static int omap_gem_attach_pages(struct drm_gem_object *obj) |
| 242 | { |
Rob Clark | 8b6b569 | 2012-05-17 02:37:25 -0600 | [diff] [blame] | 243 | struct drm_device *dev = obj->dev; |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 244 | struct omap_gem_object *omap_obj = to_omap_bo(obj); |
| 245 | struct page **pages; |
Emil Goode | d4eb23a | 2012-08-17 18:53:26 +0200 | [diff] [blame] | 246 | int npages = obj->size >> PAGE_SHIFT; |
| 247 | int i, ret; |
Rob Clark | 8b6b569 | 2012-05-17 02:37:25 -0600 | [diff] [blame] | 248 | dma_addr_t *addrs; |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 249 | |
| 250 | WARN_ON(omap_obj->pages); |
| 251 | |
David Herrmann | 0cdbe8a | 2014-05-25 12:59:47 +0200 | [diff] [blame] | 252 | pages = drm_gem_get_pages(obj); |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 253 | if (IS_ERR(pages)) { |
| 254 | dev_err(obj->dev->dev, "could not get pages: %ld\n", PTR_ERR(pages)); |
| 255 | return PTR_ERR(pages); |
| 256 | } |
| 257 | |
Rob Clark | f3bc9d2 | 2011-12-20 16:54:28 -0600 | [diff] [blame] | 258 | /* for non-cached buffers, ensure the new pages are clean because |
| 259 | * DSS, GPU, etc. are not cache coherent: |
| 260 | */ |
| 261 | if (omap_obj->flags & (OMAP_BO_WC|OMAP_BO_UNCACHED)) { |
Vincent Penquerc'h | 23d84ed | 2012-10-09 19:40:39 +0100 | [diff] [blame] | 262 | addrs = kmalloc(npages * sizeof(*addrs), GFP_KERNEL); |
Emil Goode | d4eb23a | 2012-08-17 18:53:26 +0200 | [diff] [blame] | 263 | if (!addrs) { |
| 264 | ret = -ENOMEM; |
| 265 | goto free_pages; |
| 266 | } |
| 267 | |
Rob Clark | f3bc9d2 | 2011-12-20 16:54:28 -0600 | [diff] [blame] | 268 | for (i = 0; i < npages; i++) { |
Rob Clark | 8b6b569 | 2012-05-17 02:37:25 -0600 | [diff] [blame] | 269 | addrs[i] = dma_map_page(dev->dev, pages[i], |
Rob Clark | f3bc9d2 | 2011-12-20 16:54:28 -0600 | [diff] [blame] | 270 | 0, PAGE_SIZE, DMA_BIDIRECTIONAL); |
Tomi Valkeinen | 579ef25 | 2016-01-05 11:43:14 +0200 | [diff] [blame] | 271 | |
| 272 | if (dma_mapping_error(dev->dev, addrs[i])) { |
| 273 | dev_warn(dev->dev, |
| 274 | "%s: failed to map page\n", __func__); |
| 275 | |
| 276 | for (i = i - 1; i >= 0; --i) { |
| 277 | dma_unmap_page(dev->dev, addrs[i], |
| 278 | PAGE_SIZE, DMA_BIDIRECTIONAL); |
| 279 | } |
| 280 | |
| 281 | ret = -ENOMEM; |
| 282 | goto free_addrs; |
| 283 | } |
Rob Clark | f3bc9d2 | 2011-12-20 16:54:28 -0600 | [diff] [blame] | 284 | } |
Rob Clark | 8b6b569 | 2012-05-17 02:37:25 -0600 | [diff] [blame] | 285 | } else { |
Vincent Penquerc'h | 23d84ed | 2012-10-09 19:40:39 +0100 | [diff] [blame] | 286 | addrs = kzalloc(npages * sizeof(*addrs), GFP_KERNEL); |
Emil Goode | d4eb23a | 2012-08-17 18:53:26 +0200 | [diff] [blame] | 287 | if (!addrs) { |
| 288 | ret = -ENOMEM; |
| 289 | goto free_pages; |
| 290 | } |
Rob Clark | f3bc9d2 | 2011-12-20 16:54:28 -0600 | [diff] [blame] | 291 | } |
| 292 | |
Rob Clark | 8b6b569 | 2012-05-17 02:37:25 -0600 | [diff] [blame] | 293 | omap_obj->addrs = addrs; |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 294 | omap_obj->pages = pages; |
Rob Clark | 8b6b569 | 2012-05-17 02:37:25 -0600 | [diff] [blame] | 295 | |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 296 | return 0; |
Emil Goode | d4eb23a | 2012-08-17 18:53:26 +0200 | [diff] [blame] | 297 | |
Tomi Valkeinen | 579ef25 | 2016-01-05 11:43:14 +0200 | [diff] [blame] | 298 | free_addrs: |
| 299 | kfree(addrs); |
Emil Goode | d4eb23a | 2012-08-17 18:53:26 +0200 | [diff] [blame] | 300 | free_pages: |
Rob Clark | ddcd09d | 2013-08-07 13:41:27 -0400 | [diff] [blame] | 301 | drm_gem_put_pages(obj, pages, true, false); |
Emil Goode | d4eb23a | 2012-08-17 18:53:26 +0200 | [diff] [blame] | 302 | |
| 303 | return ret; |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 304 | } |
| 305 | |
Laurent Pinchart | b902f8f | 2015-12-14 22:39:32 +0200 | [diff] [blame] | 306 | /* acquire pages when needed (for example, for DMA where physically |
| 307 | * contiguous buffer is not required |
| 308 | */ |
| 309 | static int get_pages(struct drm_gem_object *obj, struct page ***pages) |
| 310 | { |
| 311 | struct omap_gem_object *omap_obj = to_omap_bo(obj); |
| 312 | int ret = 0; |
| 313 | |
Laurent Pinchart | cdb0381 | 2015-12-14 22:39:37 +0200 | [diff] [blame] | 314 | if ((omap_obj->flags & OMAP_BO_MEM_SHMEM) && !omap_obj->pages) { |
Laurent Pinchart | b902f8f | 2015-12-14 22:39:32 +0200 | [diff] [blame] | 315 | ret = omap_gem_attach_pages(obj); |
| 316 | if (ret) { |
| 317 | dev_err(obj->dev->dev, "could not attach pages\n"); |
| 318 | return ret; |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | /* TODO: even phys-contig.. we should have a list of pages? */ |
| 323 | *pages = omap_obj->pages; |
| 324 | |
| 325 | return 0; |
| 326 | } |
| 327 | |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 328 | /** release backing pages */ |
| 329 | static void omap_gem_detach_pages(struct drm_gem_object *obj) |
| 330 | { |
| 331 | struct omap_gem_object *omap_obj = to_omap_bo(obj); |
Rob Clark | f3bc9d2 | 2011-12-20 16:54:28 -0600 | [diff] [blame] | 332 | |
| 333 | /* for non-cached buffers, ensure the new pages are clean because |
| 334 | * DSS, GPU, etc. are not cache coherent: |
| 335 | */ |
| 336 | if (omap_obj->flags & (OMAP_BO_WC|OMAP_BO_UNCACHED)) { |
| 337 | int i, npages = obj->size >> PAGE_SHIFT; |
| 338 | for (i = 0; i < npages; i++) { |
| 339 | dma_unmap_page(obj->dev->dev, omap_obj->addrs[i], |
| 340 | PAGE_SIZE, DMA_BIDIRECTIONAL); |
| 341 | } |
Rob Clark | f3bc9d2 | 2011-12-20 16:54:28 -0600 | [diff] [blame] | 342 | } |
| 343 | |
Rob Clark | 8b6b569 | 2012-05-17 02:37:25 -0600 | [diff] [blame] | 344 | kfree(omap_obj->addrs); |
| 345 | omap_obj->addrs = NULL; |
| 346 | |
Rob Clark | ddcd09d | 2013-08-07 13:41:27 -0400 | [diff] [blame] | 347 | drm_gem_put_pages(obj, omap_obj->pages, true, false); |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 348 | omap_obj->pages = NULL; |
| 349 | } |
| 350 | |
Rob Clark | 6ad11bc | 2012-04-10 13:19:55 -0500 | [diff] [blame] | 351 | /* get buffer flags */ |
| 352 | uint32_t omap_gem_flags(struct drm_gem_object *obj) |
| 353 | { |
| 354 | return to_omap_bo(obj)->flags; |
| 355 | } |
| 356 | |
Rob Clark | c5b1247 | 2012-01-18 18:33:02 -0600 | [diff] [blame] | 357 | uint64_t omap_gem_mmap_offset(struct drm_gem_object *obj) |
| 358 | { |
| 359 | uint64_t offset; |
| 360 | mutex_lock(&obj->dev->struct_mutex); |
| 361 | offset = mmap_offset(obj); |
| 362 | mutex_unlock(&obj->dev->struct_mutex); |
| 363 | return offset; |
| 364 | } |
| 365 | |
Rob Clark | f7f9f45 | 2011-12-05 19:19:22 -0600 | [diff] [blame] | 366 | /** get mmap size */ |
| 367 | size_t omap_gem_mmap_size(struct drm_gem_object *obj) |
| 368 | { |
| 369 | struct omap_gem_object *omap_obj = to_omap_bo(obj); |
| 370 | size_t size = obj->size; |
| 371 | |
| 372 | if (omap_obj->flags & OMAP_BO_TILED) { |
| 373 | /* for tiled buffers, the virtual size has stride rounded up |
| 374 | * to 4kb.. (to hide the fact that row n+1 might start 16kb or |
| 375 | * 32kb later!). But we don't back the entire buffer with |
| 376 | * pages, only the valid picture part.. so need to adjust for |
| 377 | * this in the size used to mmap and generate mmap offset |
| 378 | */ |
| 379 | size = tiler_vsize(gem2fmt(omap_obj->flags), |
| 380 | omap_obj->width, omap_obj->height); |
| 381 | } |
| 382 | |
| 383 | return size; |
| 384 | } |
| 385 | |
Rob Clark | 3c810c6 | 2012-08-15 15:18:01 -0500 | [diff] [blame] | 386 | /* get tiled size, returns -EINVAL if not tiled buffer */ |
| 387 | int omap_gem_tiled_size(struct drm_gem_object *obj, uint16_t *w, uint16_t *h) |
| 388 | { |
| 389 | struct omap_gem_object *omap_obj = to_omap_bo(obj); |
| 390 | if (omap_obj->flags & OMAP_BO_TILED) { |
| 391 | *w = omap_obj->width; |
| 392 | *h = omap_obj->height; |
| 393 | return 0; |
| 394 | } |
| 395 | return -EINVAL; |
| 396 | } |
Rob Clark | f7f9f45 | 2011-12-05 19:19:22 -0600 | [diff] [blame] | 397 | |
Laurent Pinchart | 7ef93b0 | 2015-12-14 22:39:33 +0200 | [diff] [blame] | 398 | /* ----------------------------------------------------------------------------- |
| 399 | * Fault Handling |
| 400 | */ |
| 401 | |
Rob Clark | f7f9f45 | 2011-12-05 19:19:22 -0600 | [diff] [blame] | 402 | /* Normal handling for the case of faulting in non-tiled buffers */ |
| 403 | static int fault_1d(struct drm_gem_object *obj, |
| 404 | struct vm_area_struct *vma, struct vm_fault *vmf) |
| 405 | { |
| 406 | struct omap_gem_object *omap_obj = to_omap_bo(obj); |
| 407 | unsigned long pfn; |
| 408 | pgoff_t pgoff; |
| 409 | |
| 410 | /* We don't use vmf->pgoff since that has the fake offset: */ |
| 411 | pgoff = ((unsigned long)vmf->virtual_address - |
| 412 | vma->vm_start) >> PAGE_SHIFT; |
| 413 | |
| 414 | if (omap_obj->pages) { |
Rob Clark | 8b6b569 | 2012-05-17 02:37:25 -0600 | [diff] [blame] | 415 | omap_gem_cpu_sync(obj, pgoff); |
Rob Clark | f7f9f45 | 2011-12-05 19:19:22 -0600 | [diff] [blame] | 416 | pfn = page_to_pfn(omap_obj->pages[pgoff]); |
| 417 | } else { |
Laurent Pinchart | b22e669 | 2015-12-14 22:39:44 +0200 | [diff] [blame] | 418 | BUG_ON(!is_contiguous(omap_obj)); |
Rob Clark | f7f9f45 | 2011-12-05 19:19:22 -0600 | [diff] [blame] | 419 | pfn = (omap_obj->paddr >> PAGE_SHIFT) + pgoff; |
| 420 | } |
| 421 | |
| 422 | VERB("Inserting %p pfn %lx, pa %lx", vmf->virtual_address, |
| 423 | pfn, pfn << PAGE_SHIFT); |
| 424 | |
Dan Williams | 01c8f1c | 2016-01-15 16:56:40 -0800 | [diff] [blame] | 425 | return vm_insert_mixed(vma, (unsigned long)vmf->virtual_address, |
| 426 | __pfn_to_pfn_t(pfn, PFN_DEV)); |
Rob Clark | f7f9f45 | 2011-12-05 19:19:22 -0600 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | /* Special handling for the case of faulting in 2d tiled buffers */ |
| 430 | static int fault_2d(struct drm_gem_object *obj, |
| 431 | struct vm_area_struct *vma, struct vm_fault *vmf) |
| 432 | { |
| 433 | struct omap_gem_object *omap_obj = to_omap_bo(obj); |
Laurent Pinchart | f430274 | 2015-12-14 22:39:34 +0200 | [diff] [blame] | 434 | struct omap_drm_private *priv = obj->dev->dev_private; |
| 435 | struct omap_drm_usergart_entry *entry; |
Rob Clark | f7f9f45 | 2011-12-05 19:19:22 -0600 | [diff] [blame] | 436 | enum tiler_fmt fmt = gem2fmt(omap_obj->flags); |
| 437 | struct page *pages[64]; /* XXX is this too much to have on stack? */ |
| 438 | unsigned long pfn; |
| 439 | pgoff_t pgoff, base_pgoff; |
| 440 | void __user *vaddr; |
| 441 | int i, ret, slots; |
| 442 | |
Rob Clark | e559895 | 2012-03-05 10:48:40 -0600 | [diff] [blame] | 443 | /* |
| 444 | * Note the height of the slot is also equal to the number of pages |
| 445 | * that need to be mapped in to fill 4kb wide CPU page. If the slot |
| 446 | * height is 64, then 64 pages fill a 4kb wide by 64 row region. |
Rob Clark | f7f9f45 | 2011-12-05 19:19:22 -0600 | [diff] [blame] | 447 | */ |
Laurent Pinchart | f430274 | 2015-12-14 22:39:34 +0200 | [diff] [blame] | 448 | const int n = priv->usergart[fmt].height; |
| 449 | const int n_shift = priv->usergart[fmt].height_shift; |
Rob Clark | e559895 | 2012-03-05 10:48:40 -0600 | [diff] [blame] | 450 | |
| 451 | /* |
| 452 | * If buffer width in bytes > PAGE_SIZE then the virtual stride is |
| 453 | * rounded up to next multiple of PAGE_SIZE.. this need to be taken |
| 454 | * into account in some of the math, so figure out virtual stride |
| 455 | * in pages |
| 456 | */ |
| 457 | const int m = 1 + ((omap_obj->width << fmt) / PAGE_SIZE); |
Rob Clark | f7f9f45 | 2011-12-05 19:19:22 -0600 | [diff] [blame] | 458 | |
| 459 | /* We don't use vmf->pgoff since that has the fake offset: */ |
| 460 | pgoff = ((unsigned long)vmf->virtual_address - |
| 461 | vma->vm_start) >> PAGE_SHIFT; |
| 462 | |
Rob Clark | e559895 | 2012-03-05 10:48:40 -0600 | [diff] [blame] | 463 | /* |
| 464 | * Actual address we start mapping at is rounded down to previous slot |
Rob Clark | f7f9f45 | 2011-12-05 19:19:22 -0600 | [diff] [blame] | 465 | * boundary in the y direction: |
| 466 | */ |
Rob Clark | e559895 | 2012-03-05 10:48:40 -0600 | [diff] [blame] | 467 | base_pgoff = round_down(pgoff, m << n_shift); |
Rob Clark | f7f9f45 | 2011-12-05 19:19:22 -0600 | [diff] [blame] | 468 | |
Rob Clark | e559895 | 2012-03-05 10:48:40 -0600 | [diff] [blame] | 469 | /* figure out buffer width in slots */ |
Laurent Pinchart | f430274 | 2015-12-14 22:39:34 +0200 | [diff] [blame] | 470 | slots = omap_obj->width >> priv->usergart[fmt].slot_shift; |
Rob Clark | f7f9f45 | 2011-12-05 19:19:22 -0600 | [diff] [blame] | 471 | |
Rob Clark | e559895 | 2012-03-05 10:48:40 -0600 | [diff] [blame] | 472 | vaddr = vmf->virtual_address - ((pgoff - base_pgoff) << PAGE_SHIFT); |
| 473 | |
Laurent Pinchart | f430274 | 2015-12-14 22:39:34 +0200 | [diff] [blame] | 474 | entry = &priv->usergart[fmt].entry[priv->usergart[fmt].last]; |
Rob Clark | e559895 | 2012-03-05 10:48:40 -0600 | [diff] [blame] | 475 | |
Rob Clark | f7f9f45 | 2011-12-05 19:19:22 -0600 | [diff] [blame] | 476 | /* evict previous buffer using this usergart entry, if any: */ |
| 477 | if (entry->obj) |
| 478 | evict_entry(entry->obj, fmt, entry); |
| 479 | |
| 480 | entry->obj = obj; |
| 481 | entry->obj_pgoff = base_pgoff; |
| 482 | |
Rob Clark | e559895 | 2012-03-05 10:48:40 -0600 | [diff] [blame] | 483 | /* now convert base_pgoff to phys offset from virt offset: */ |
| 484 | base_pgoff = (base_pgoff >> n_shift) * slots; |
Rob Clark | f7f9f45 | 2011-12-05 19:19:22 -0600 | [diff] [blame] | 485 | |
Rob Clark | e559895 | 2012-03-05 10:48:40 -0600 | [diff] [blame] | 486 | /* for wider-than 4k.. figure out which part of the slot-row we want: */ |
| 487 | if (m > 1) { |
| 488 | int off = pgoff % m; |
| 489 | entry->obj_pgoff += off; |
| 490 | base_pgoff /= m; |
| 491 | slots = min(slots - (off << n_shift), n); |
| 492 | base_pgoff += off << n_shift; |
| 493 | vaddr += off << PAGE_SHIFT; |
| 494 | } |
| 495 | |
| 496 | /* |
| 497 | * Map in pages. Beyond the valid pixel part of the buffer, we set |
| 498 | * pages[i] to NULL to get a dummy page mapped in.. if someone |
| 499 | * reads/writes it they will get random/undefined content, but at |
| 500 | * least it won't be corrupting whatever other random page used to |
| 501 | * be mapped in, or other undefined behavior. |
Rob Clark | f7f9f45 | 2011-12-05 19:19:22 -0600 | [diff] [blame] | 502 | */ |
| 503 | memcpy(pages, &omap_obj->pages[base_pgoff], |
| 504 | sizeof(struct page *) * slots); |
| 505 | memset(pages + slots, 0, |
Rob Clark | e559895 | 2012-03-05 10:48:40 -0600 | [diff] [blame] | 506 | sizeof(struct page *) * (n - slots)); |
Rob Clark | f7f9f45 | 2011-12-05 19:19:22 -0600 | [diff] [blame] | 507 | |
Rob Clark | a6a9182 | 2011-12-09 23:26:08 -0600 | [diff] [blame] | 508 | ret = tiler_pin(entry->block, pages, ARRAY_SIZE(pages), 0, true); |
Rob Clark | f7f9f45 | 2011-12-05 19:19:22 -0600 | [diff] [blame] | 509 | if (ret) { |
| 510 | dev_err(obj->dev->dev, "failed to pin: %d\n", ret); |
| 511 | return ret; |
| 512 | } |
| 513 | |
Rob Clark | f7f9f45 | 2011-12-05 19:19:22 -0600 | [diff] [blame] | 514 | pfn = entry->paddr >> PAGE_SHIFT; |
| 515 | |
| 516 | VERB("Inserting %p pfn %lx, pa %lx", vmf->virtual_address, |
| 517 | pfn, pfn << PAGE_SHIFT); |
| 518 | |
Rob Clark | e559895 | 2012-03-05 10:48:40 -0600 | [diff] [blame] | 519 | for (i = n; i > 0; i--) { |
Dan Williams | 01c8f1c | 2016-01-15 16:56:40 -0800 | [diff] [blame] | 520 | vm_insert_mixed(vma, (unsigned long)vaddr, |
| 521 | __pfn_to_pfn_t(pfn, PFN_DEV)); |
Laurent Pinchart | f430274 | 2015-12-14 22:39:34 +0200 | [diff] [blame] | 522 | pfn += priv->usergart[fmt].stride_pfn; |
Rob Clark | e559895 | 2012-03-05 10:48:40 -0600 | [diff] [blame] | 523 | vaddr += PAGE_SIZE * m; |
Rob Clark | f7f9f45 | 2011-12-05 19:19:22 -0600 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | /* simple round-robin: */ |
Laurent Pinchart | f430274 | 2015-12-14 22:39:34 +0200 | [diff] [blame] | 527 | priv->usergart[fmt].last = (priv->usergart[fmt].last + 1) |
| 528 | % NUM_USERGART_ENTRIES; |
Rob Clark | f7f9f45 | 2011-12-05 19:19:22 -0600 | [diff] [blame] | 529 | |
| 530 | return 0; |
| 531 | } |
| 532 | |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 533 | /** |
| 534 | * omap_gem_fault - pagefault handler for GEM objects |
| 535 | * @vma: the VMA of the GEM object |
| 536 | * @vmf: fault detail |
| 537 | * |
| 538 | * Invoked when a fault occurs on an mmap of a GEM managed area. GEM |
| 539 | * does most of the work for us including the actual map/unmap calls |
| 540 | * but we need to do the actual page work. |
| 541 | * |
| 542 | * The VMA was set up by GEM. In doing so it also ensured that the |
| 543 | * vma->vm_private_data points to the GEM object that is backing this |
| 544 | * mapping. |
| 545 | */ |
| 546 | int omap_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf) |
| 547 | { |
| 548 | struct drm_gem_object *obj = vma->vm_private_data; |
| 549 | struct omap_gem_object *omap_obj = to_omap_bo(obj); |
| 550 | struct drm_device *dev = obj->dev; |
| 551 | struct page **pages; |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 552 | int ret; |
| 553 | |
| 554 | /* Make sure we don't parallel update on a fault, nor move or remove |
| 555 | * something from beneath our feet |
| 556 | */ |
| 557 | mutex_lock(&dev->struct_mutex); |
| 558 | |
| 559 | /* if a shmem backed object, make sure we have pages attached now */ |
| 560 | ret = get_pages(obj, &pages); |
YAMANE Toshiaki | ae05303 | 2012-11-14 19:33:17 +0900 | [diff] [blame] | 561 | if (ret) |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 562 | goto fail; |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 563 | |
| 564 | /* where should we do corresponding put_pages().. we are mapping |
| 565 | * the original page, rather than thru a GART, so we can't rely |
| 566 | * on eviction to trigger this. But munmap() or all mappings should |
| 567 | * probably trigger put_pages()? |
| 568 | */ |
| 569 | |
Rob Clark | f7f9f45 | 2011-12-05 19:19:22 -0600 | [diff] [blame] | 570 | if (omap_obj->flags & OMAP_BO_TILED) |
| 571 | ret = fault_2d(obj, vma, vmf); |
| 572 | else |
| 573 | ret = fault_1d(obj, vma, vmf); |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 574 | |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 575 | |
| 576 | fail: |
| 577 | mutex_unlock(&dev->struct_mutex); |
| 578 | switch (ret) { |
| 579 | case 0: |
| 580 | case -ERESTARTSYS: |
| 581 | case -EINTR: |
Rob Clark | e1d4ee0 | 2013-10-20 12:07:42 -0400 | [diff] [blame] | 582 | case -EBUSY: |
| 583 | /* |
| 584 | * EBUSY is ok: this just means that another thread |
| 585 | * already did the job. |
| 586 | */ |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 587 | return VM_FAULT_NOPAGE; |
| 588 | case -ENOMEM: |
| 589 | return VM_FAULT_OOM; |
| 590 | default: |
| 591 | return VM_FAULT_SIGBUS; |
| 592 | } |
| 593 | } |
| 594 | |
| 595 | /** We override mainly to fix up some of the vm mapping flags.. */ |
| 596 | int omap_gem_mmap(struct file *filp, struct vm_area_struct *vma) |
| 597 | { |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 598 | int ret; |
| 599 | |
| 600 | ret = drm_gem_mmap(filp, vma); |
| 601 | if (ret) { |
| 602 | DBG("mmap failed: %d", ret); |
| 603 | return ret; |
| 604 | } |
| 605 | |
Rob Clark | 8b6b569 | 2012-05-17 02:37:25 -0600 | [diff] [blame] | 606 | return omap_gem_mmap_obj(vma->vm_private_data, vma); |
| 607 | } |
| 608 | |
| 609 | int omap_gem_mmap_obj(struct drm_gem_object *obj, |
| 610 | struct vm_area_struct *vma) |
| 611 | { |
| 612 | struct omap_gem_object *omap_obj = to_omap_bo(obj); |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 613 | |
| 614 | vma->vm_flags &= ~VM_PFNMAP; |
| 615 | vma->vm_flags |= VM_MIXEDMAP; |
| 616 | |
| 617 | if (omap_obj->flags & OMAP_BO_WC) { |
| 618 | vma->vm_page_prot = pgprot_writecombine(vm_get_page_prot(vma->vm_flags)); |
| 619 | } else if (omap_obj->flags & OMAP_BO_UNCACHED) { |
| 620 | vma->vm_page_prot = pgprot_noncached(vm_get_page_prot(vma->vm_flags)); |
| 621 | } else { |
Rob Clark | 8b6b569 | 2012-05-17 02:37:25 -0600 | [diff] [blame] | 622 | /* |
| 623 | * We do have some private objects, at least for scanout buffers |
| 624 | * on hardware without DMM/TILER. But these are allocated write- |
| 625 | * combine |
| 626 | */ |
| 627 | if (WARN_ON(!obj->filp)) |
| 628 | return -EINVAL; |
| 629 | |
| 630 | /* |
| 631 | * Shunt off cached objs to shmem file so they have their own |
| 632 | * address_space (so unmap_mapping_range does what we want, |
| 633 | * in particular in the case of mmap'd dmabufs) |
| 634 | */ |
| 635 | fput(vma->vm_file); |
Rob Clark | 8b6b569 | 2012-05-17 02:37:25 -0600 | [diff] [blame] | 636 | vma->vm_pgoff = 0; |
Al Viro | cb0942b | 2012-08-27 14:48:26 -0400 | [diff] [blame] | 637 | vma->vm_file = get_file(obj->filp); |
Rob Clark | 8b6b569 | 2012-05-17 02:37:25 -0600 | [diff] [blame] | 638 | |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 639 | vma->vm_page_prot = vm_get_page_prot(vma->vm_flags); |
| 640 | } |
| 641 | |
Rob Clark | 8b6b569 | 2012-05-17 02:37:25 -0600 | [diff] [blame] | 642 | return 0; |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 643 | } |
| 644 | |
Laurent Pinchart | 7ef93b0 | 2015-12-14 22:39:33 +0200 | [diff] [blame] | 645 | /* ----------------------------------------------------------------------------- |
| 646 | * Dumb Buffers |
| 647 | */ |
Rob Clark | 8b6b569 | 2012-05-17 02:37:25 -0600 | [diff] [blame] | 648 | |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 649 | /** |
| 650 | * omap_gem_dumb_create - create a dumb buffer |
| 651 | * @drm_file: our client file |
| 652 | * @dev: our device |
| 653 | * @args: the requested arguments copied from userspace |
| 654 | * |
| 655 | * Allocate a buffer suitable for use for a frame buffer of the |
| 656 | * form described by user space. Give userspace a handle by which |
| 657 | * to reference it. |
| 658 | */ |
| 659 | int omap_gem_dumb_create(struct drm_file *file, struct drm_device *dev, |
| 660 | struct drm_mode_create_dumb *args) |
| 661 | { |
| 662 | union omap_gem_size gsize; |
| 663 | |
Thierry Reding | bdb2b93 | 2014-11-03 11:57:33 +0100 | [diff] [blame] | 664 | args->pitch = align_pitch(0, args->width, args->bpp); |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 665 | args->size = PAGE_ALIGN(args->pitch * args->height); |
| 666 | |
| 667 | gsize = (union omap_gem_size){ |
| 668 | .bytes = args->size, |
| 669 | }; |
| 670 | |
| 671 | return omap_gem_new_handle(dev, file, gsize, |
| 672 | OMAP_BO_SCANOUT | OMAP_BO_WC, &args->handle); |
| 673 | } |
| 674 | |
| 675 | /** |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 676 | * omap_gem_dumb_map - buffer mapping for dumb interface |
| 677 | * @file: our drm client file |
| 678 | * @dev: drm device |
| 679 | * @handle: GEM handle to the object (from dumb_create) |
| 680 | * |
| 681 | * Do the necessary setup to allow the mapping of the frame buffer |
| 682 | * into user memory. We don't have to do much here at the moment. |
| 683 | */ |
| 684 | int omap_gem_dumb_map_offset(struct drm_file *file, struct drm_device *dev, |
| 685 | uint32_t handle, uint64_t *offset) |
| 686 | { |
| 687 | struct drm_gem_object *obj; |
| 688 | int ret = 0; |
| 689 | |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 690 | /* GEM does all our handle to object mapping */ |
Chris Wilson | a8ad0bd | 2016-05-09 11:04:54 +0100 | [diff] [blame] | 691 | obj = drm_gem_object_lookup(file, handle); |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 692 | if (obj == NULL) { |
| 693 | ret = -ENOENT; |
| 694 | goto fail; |
| 695 | } |
| 696 | |
| 697 | *offset = omap_gem_mmap_offset(obj); |
| 698 | |
| 699 | drm_gem_object_unreference_unlocked(obj); |
| 700 | |
| 701 | fail: |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 702 | return ret; |
| 703 | } |
| 704 | |
Laurent Pinchart | e1c1174 | 2015-12-14 22:39:30 +0200 | [diff] [blame] | 705 | #ifdef CONFIG_DRM_FBDEV_EMULATION |
Rob Clark | a6a9182 | 2011-12-09 23:26:08 -0600 | [diff] [blame] | 706 | /* Set scrolling position. This allows us to implement fast scrolling |
| 707 | * for console. |
Rob Clark | 9b55b95 | 2012-03-05 10:48:33 -0600 | [diff] [blame] | 708 | * |
| 709 | * Call only from non-atomic contexts. |
Rob Clark | a6a9182 | 2011-12-09 23:26:08 -0600 | [diff] [blame] | 710 | */ |
| 711 | int omap_gem_roll(struct drm_gem_object *obj, uint32_t roll) |
| 712 | { |
| 713 | struct omap_gem_object *omap_obj = to_omap_bo(obj); |
| 714 | uint32_t npages = obj->size >> PAGE_SHIFT; |
| 715 | int ret = 0; |
| 716 | |
| 717 | if (roll > npages) { |
| 718 | dev_err(obj->dev->dev, "invalid roll: %d\n", roll); |
| 719 | return -EINVAL; |
| 720 | } |
| 721 | |
Rob Clark | a6a9182 | 2011-12-09 23:26:08 -0600 | [diff] [blame] | 722 | omap_obj->roll = roll; |
| 723 | |
Rob Clark | af69592 | 2011-12-16 11:34:34 -0600 | [diff] [blame] | 724 | mutex_lock(&obj->dev->struct_mutex); |
| 725 | |
Rob Clark | a6a9182 | 2011-12-09 23:26:08 -0600 | [diff] [blame] | 726 | /* if we aren't mapped yet, we don't need to do anything */ |
| 727 | if (omap_obj->block) { |
| 728 | struct page **pages; |
| 729 | ret = get_pages(obj, &pages); |
| 730 | if (ret) |
| 731 | goto fail; |
| 732 | ret = tiler_pin(omap_obj->block, pages, npages, roll, true); |
| 733 | if (ret) |
| 734 | dev_err(obj->dev->dev, "could not repin: %d\n", ret); |
| 735 | } |
| 736 | |
| 737 | fail: |
| 738 | mutex_unlock(&obj->dev->struct_mutex); |
| 739 | |
| 740 | return ret; |
| 741 | } |
Laurent Pinchart | e1c1174 | 2015-12-14 22:39:30 +0200 | [diff] [blame] | 742 | #endif |
Rob Clark | a6a9182 | 2011-12-09 23:26:08 -0600 | [diff] [blame] | 743 | |
Laurent Pinchart | 7ef93b0 | 2015-12-14 22:39:33 +0200 | [diff] [blame] | 744 | /* ----------------------------------------------------------------------------- |
| 745 | * Memory Management & DMA Sync |
| 746 | */ |
| 747 | |
| 748 | /** |
| 749 | * shmem buffers that are mapped cached can simulate coherency via using |
| 750 | * page faulting to keep track of dirty pages |
| 751 | */ |
| 752 | static inline bool is_cached_coherent(struct drm_gem_object *obj) |
| 753 | { |
| 754 | struct omap_gem_object *omap_obj = to_omap_bo(obj); |
Laurent Pinchart | cdb0381 | 2015-12-14 22:39:37 +0200 | [diff] [blame] | 755 | |
| 756 | return (omap_obj->flags & OMAP_BO_MEM_SHMEM) && |
Laurent Pinchart | 7ef93b0 | 2015-12-14 22:39:33 +0200 | [diff] [blame] | 757 | ((omap_obj->flags & OMAP_BO_CACHE_MASK) == OMAP_BO_CACHED); |
| 758 | } |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 759 | |
Rob Clark | 8b6b569 | 2012-05-17 02:37:25 -0600 | [diff] [blame] | 760 | /* Sync the buffer for CPU access.. note pages should already be |
| 761 | * attached, ie. omap_gem_get_pages() |
| 762 | */ |
| 763 | void omap_gem_cpu_sync(struct drm_gem_object *obj, int pgoff) |
| 764 | { |
| 765 | struct drm_device *dev = obj->dev; |
| 766 | struct omap_gem_object *omap_obj = to_omap_bo(obj); |
| 767 | |
| 768 | if (is_cached_coherent(obj) && omap_obj->addrs[pgoff]) { |
| 769 | dma_unmap_page(dev->dev, omap_obj->addrs[pgoff], |
| 770 | PAGE_SIZE, DMA_BIDIRECTIONAL); |
| 771 | omap_obj->addrs[pgoff] = 0; |
| 772 | } |
| 773 | } |
| 774 | |
| 775 | /* sync the buffer for DMA access */ |
| 776 | void omap_gem_dma_sync(struct drm_gem_object *obj, |
| 777 | enum dma_data_direction dir) |
| 778 | { |
| 779 | struct drm_device *dev = obj->dev; |
| 780 | struct omap_gem_object *omap_obj = to_omap_bo(obj); |
| 781 | |
| 782 | if (is_cached_coherent(obj)) { |
| 783 | int i, npages = obj->size >> PAGE_SHIFT; |
| 784 | struct page **pages = omap_obj->pages; |
| 785 | bool dirty = false; |
| 786 | |
| 787 | for (i = 0; i < npages; i++) { |
| 788 | if (!omap_obj->addrs[i]) { |
Tomi Valkeinen | a3d6345 | 2016-01-05 11:43:15 +0200 | [diff] [blame] | 789 | dma_addr_t addr; |
| 790 | |
| 791 | addr = dma_map_page(dev->dev, pages[i], 0, |
Rob Clark | 8b6b569 | 2012-05-17 02:37:25 -0600 | [diff] [blame] | 792 | PAGE_SIZE, DMA_BIDIRECTIONAL); |
Tomi Valkeinen | a3d6345 | 2016-01-05 11:43:15 +0200 | [diff] [blame] | 793 | |
| 794 | if (dma_mapping_error(dev->dev, addr)) { |
| 795 | dev_warn(dev->dev, |
| 796 | "%s: failed to map page\n", |
| 797 | __func__); |
| 798 | break; |
| 799 | } |
| 800 | |
Rob Clark | 8b6b569 | 2012-05-17 02:37:25 -0600 | [diff] [blame] | 801 | dirty = true; |
Tomi Valkeinen | a3d6345 | 2016-01-05 11:43:15 +0200 | [diff] [blame] | 802 | omap_obj->addrs[i] = addr; |
Rob Clark | 8b6b569 | 2012-05-17 02:37:25 -0600 | [diff] [blame] | 803 | } |
| 804 | } |
| 805 | |
| 806 | if (dirty) { |
| 807 | unmap_mapping_range(obj->filp->f_mapping, 0, |
| 808 | omap_gem_mmap_size(obj), 1); |
| 809 | } |
| 810 | } |
| 811 | } |
| 812 | |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 813 | /* Get physical address for DMA.. if 'remap' is true, and the buffer is not |
| 814 | * already contiguous, remap it to pin in physically contiguous memory.. (ie. |
| 815 | * map in TILER) |
| 816 | */ |
| 817 | int omap_gem_get_paddr(struct drm_gem_object *obj, |
| 818 | dma_addr_t *paddr, bool remap) |
| 819 | { |
Rob Clark | a6a9182 | 2011-12-09 23:26:08 -0600 | [diff] [blame] | 820 | struct omap_drm_private *priv = obj->dev->dev_private; |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 821 | struct omap_gem_object *omap_obj = to_omap_bo(obj); |
| 822 | int ret = 0; |
| 823 | |
Rob Clark | f7f9f45 | 2011-12-05 19:19:22 -0600 | [diff] [blame] | 824 | mutex_lock(&obj->dev->struct_mutex); |
| 825 | |
Laurent Pinchart | b22e669 | 2015-12-14 22:39:44 +0200 | [diff] [blame] | 826 | if (!is_contiguous(omap_obj) && remap && priv->has_dmm) { |
Rob Clark | f7f9f45 | 2011-12-05 19:19:22 -0600 | [diff] [blame] | 827 | if (omap_obj->paddr_cnt == 0) { |
| 828 | struct page **pages; |
Rob Clark | a6a9182 | 2011-12-09 23:26:08 -0600 | [diff] [blame] | 829 | uint32_t npages = obj->size >> PAGE_SHIFT; |
Rob Clark | f7f9f45 | 2011-12-05 19:19:22 -0600 | [diff] [blame] | 830 | enum tiler_fmt fmt = gem2fmt(omap_obj->flags); |
| 831 | struct tiler_block *block; |
Rob Clark | a6a9182 | 2011-12-09 23:26:08 -0600 | [diff] [blame] | 832 | |
Rob Clark | f7f9f45 | 2011-12-05 19:19:22 -0600 | [diff] [blame] | 833 | BUG_ON(omap_obj->block); |
| 834 | |
| 835 | ret = get_pages(obj, &pages); |
| 836 | if (ret) |
| 837 | goto fail; |
| 838 | |
Rob Clark | f7f9f45 | 2011-12-05 19:19:22 -0600 | [diff] [blame] | 839 | if (omap_obj->flags & OMAP_BO_TILED) { |
| 840 | block = tiler_reserve_2d(fmt, |
| 841 | omap_obj->width, |
| 842 | omap_obj->height, 0); |
| 843 | } else { |
| 844 | block = tiler_reserve_1d(obj->size); |
| 845 | } |
| 846 | |
| 847 | if (IS_ERR(block)) { |
| 848 | ret = PTR_ERR(block); |
| 849 | dev_err(obj->dev->dev, |
| 850 | "could not remap: %d (%d)\n", ret, fmt); |
| 851 | goto fail; |
| 852 | } |
| 853 | |
| 854 | /* TODO: enable async refill.. */ |
Rob Clark | a6a9182 | 2011-12-09 23:26:08 -0600 | [diff] [blame] | 855 | ret = tiler_pin(block, pages, npages, |
| 856 | omap_obj->roll, true); |
Rob Clark | f7f9f45 | 2011-12-05 19:19:22 -0600 | [diff] [blame] | 857 | if (ret) { |
| 858 | tiler_release(block); |
| 859 | dev_err(obj->dev->dev, |
| 860 | "could not pin: %d\n", ret); |
| 861 | goto fail; |
| 862 | } |
| 863 | |
| 864 | omap_obj->paddr = tiler_ssptr(block); |
| 865 | omap_obj->block = block; |
| 866 | |
Russell King | 2d31ca3 | 2014-07-12 10:53:41 +0100 | [diff] [blame] | 867 | DBG("got paddr: %pad", &omap_obj->paddr); |
Rob Clark | f7f9f45 | 2011-12-05 19:19:22 -0600 | [diff] [blame] | 868 | } |
| 869 | |
| 870 | omap_obj->paddr_cnt++; |
| 871 | |
| 872 | *paddr = omap_obj->paddr; |
Laurent Pinchart | b22e669 | 2015-12-14 22:39:44 +0200 | [diff] [blame] | 873 | } else if (is_contiguous(omap_obj)) { |
Rob Clark | f7f9f45 | 2011-12-05 19:19:22 -0600 | [diff] [blame] | 874 | *paddr = omap_obj->paddr; |
| 875 | } else { |
| 876 | ret = -EINVAL; |
Rob Clark | 8b6b569 | 2012-05-17 02:37:25 -0600 | [diff] [blame] | 877 | goto fail; |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 878 | } |
| 879 | |
Rob Clark | f7f9f45 | 2011-12-05 19:19:22 -0600 | [diff] [blame] | 880 | fail: |
| 881 | mutex_unlock(&obj->dev->struct_mutex); |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 882 | |
| 883 | return ret; |
| 884 | } |
| 885 | |
| 886 | /* Release physical address, when DMA is no longer being performed.. this |
| 887 | * could potentially unpin and unmap buffers from TILER |
| 888 | */ |
Tomi Valkeinen | 393a949 | 2015-04-28 14:01:36 +0300 | [diff] [blame] | 889 | void omap_gem_put_paddr(struct drm_gem_object *obj) |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 890 | { |
Rob Clark | f7f9f45 | 2011-12-05 19:19:22 -0600 | [diff] [blame] | 891 | struct omap_gem_object *omap_obj = to_omap_bo(obj); |
Tomi Valkeinen | 393a949 | 2015-04-28 14:01:36 +0300 | [diff] [blame] | 892 | int ret; |
Rob Clark | f7f9f45 | 2011-12-05 19:19:22 -0600 | [diff] [blame] | 893 | |
| 894 | mutex_lock(&obj->dev->struct_mutex); |
| 895 | if (omap_obj->paddr_cnt > 0) { |
| 896 | omap_obj->paddr_cnt--; |
| 897 | if (omap_obj->paddr_cnt == 0) { |
| 898 | ret = tiler_unpin(omap_obj->block); |
| 899 | if (ret) { |
| 900 | dev_err(obj->dev->dev, |
| 901 | "could not unpin pages: %d\n", ret); |
Rob Clark | f7f9f45 | 2011-12-05 19:19:22 -0600 | [diff] [blame] | 902 | } |
| 903 | ret = tiler_release(omap_obj->block); |
| 904 | if (ret) { |
| 905 | dev_err(obj->dev->dev, |
| 906 | "could not release unmap: %d\n", ret); |
| 907 | } |
Tomi Valkeinen | 3f4d17c | 2014-09-03 19:25:53 +0000 | [diff] [blame] | 908 | omap_obj->paddr = 0; |
Rob Clark | f7f9f45 | 2011-12-05 19:19:22 -0600 | [diff] [blame] | 909 | omap_obj->block = NULL; |
| 910 | } |
| 911 | } |
Tomi Valkeinen | 393a949 | 2015-04-28 14:01:36 +0300 | [diff] [blame] | 912 | |
Rob Clark | f7f9f45 | 2011-12-05 19:19:22 -0600 | [diff] [blame] | 913 | mutex_unlock(&obj->dev->struct_mutex); |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 914 | } |
| 915 | |
Rob Clark | 3c810c6 | 2012-08-15 15:18:01 -0500 | [diff] [blame] | 916 | /* Get rotated scanout address (only valid if already pinned), at the |
| 917 | * specified orientation and x,y offset from top-left corner of buffer |
| 918 | * (only valid for tiled 2d buffers) |
| 919 | */ |
| 920 | int omap_gem_rotated_paddr(struct drm_gem_object *obj, uint32_t orient, |
| 921 | int x, int y, dma_addr_t *paddr) |
| 922 | { |
| 923 | struct omap_gem_object *omap_obj = to_omap_bo(obj); |
| 924 | int ret = -EINVAL; |
| 925 | |
| 926 | mutex_lock(&obj->dev->struct_mutex); |
| 927 | if ((omap_obj->paddr_cnt > 0) && omap_obj->block && |
| 928 | (omap_obj->flags & OMAP_BO_TILED)) { |
| 929 | *paddr = tiler_tsptr(omap_obj->block, orient, x, y); |
| 930 | ret = 0; |
| 931 | } |
| 932 | mutex_unlock(&obj->dev->struct_mutex); |
| 933 | return ret; |
| 934 | } |
| 935 | |
| 936 | /* Get tiler stride for the buffer (only valid for 2d tiled buffers) */ |
| 937 | int omap_gem_tiled_stride(struct drm_gem_object *obj, uint32_t orient) |
| 938 | { |
| 939 | struct omap_gem_object *omap_obj = to_omap_bo(obj); |
| 940 | int ret = -EINVAL; |
| 941 | if (omap_obj->flags & OMAP_BO_TILED) |
| 942 | ret = tiler_stride(gem2fmt(omap_obj->flags), orient); |
| 943 | return ret; |
| 944 | } |
| 945 | |
Rob Clark | 6ad11bc | 2012-04-10 13:19:55 -0500 | [diff] [blame] | 946 | /* if !remap, and we don't have pages backing, then fail, rather than |
| 947 | * increasing the pin count (which we don't really do yet anyways, |
| 948 | * because we don't support swapping pages back out). And 'remap' |
| 949 | * might not be quite the right name, but I wanted to keep it working |
| 950 | * similarly to omap_gem_get_paddr(). Note though that mutex is not |
| 951 | * aquired if !remap (because this can be called in atomic ctxt), |
| 952 | * but probably omap_gem_get_paddr() should be changed to work in the |
| 953 | * same way. If !remap, a matching omap_gem_put_pages() call is not |
| 954 | * required (and should not be made). |
| 955 | */ |
| 956 | int omap_gem_get_pages(struct drm_gem_object *obj, struct page ***pages, |
| 957 | bool remap) |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 958 | { |
| 959 | int ret; |
Rob Clark | 6ad11bc | 2012-04-10 13:19:55 -0500 | [diff] [blame] | 960 | if (!remap) { |
| 961 | struct omap_gem_object *omap_obj = to_omap_bo(obj); |
| 962 | if (!omap_obj->pages) |
| 963 | return -ENOMEM; |
| 964 | *pages = omap_obj->pages; |
| 965 | return 0; |
| 966 | } |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 967 | mutex_lock(&obj->dev->struct_mutex); |
| 968 | ret = get_pages(obj, pages); |
| 969 | mutex_unlock(&obj->dev->struct_mutex); |
| 970 | return ret; |
| 971 | } |
| 972 | |
| 973 | /* release pages when DMA no longer being performed */ |
| 974 | int omap_gem_put_pages(struct drm_gem_object *obj) |
| 975 | { |
| 976 | /* do something here if we dynamically attach/detach pages.. at |
| 977 | * least they would no longer need to be pinned if everyone has |
| 978 | * released the pages.. |
| 979 | */ |
| 980 | return 0; |
| 981 | } |
| 982 | |
Laurent Pinchart | e1c1174 | 2015-12-14 22:39:30 +0200 | [diff] [blame] | 983 | #ifdef CONFIG_DRM_FBDEV_EMULATION |
Rob Clark | f7f9f45 | 2011-12-05 19:19:22 -0600 | [diff] [blame] | 984 | /* Get kernel virtual address for CPU access.. this more or less only |
| 985 | * exists for omap_fbdev. This should be called with struct_mutex |
| 986 | * held. |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 987 | */ |
| 988 | void *omap_gem_vaddr(struct drm_gem_object *obj) |
| 989 | { |
| 990 | struct omap_gem_object *omap_obj = to_omap_bo(obj); |
YAMANE Toshiaki | 696e3ca | 2012-11-14 19:33:43 +0900 | [diff] [blame] | 991 | WARN_ON(!mutex_is_locked(&obj->dev->struct_mutex)); |
Rob Clark | f7f9f45 | 2011-12-05 19:19:22 -0600 | [diff] [blame] | 992 | if (!omap_obj->vaddr) { |
| 993 | struct page **pages; |
| 994 | int ret = get_pages(obj, &pages); |
| 995 | if (ret) |
| 996 | return ERR_PTR(ret); |
| 997 | omap_obj->vaddr = vmap(pages, obj->size >> PAGE_SHIFT, |
| 998 | VM_MAP, pgprot_writecombine(PAGE_KERNEL)); |
| 999 | } |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 1000 | return omap_obj->vaddr; |
| 1001 | } |
Laurent Pinchart | e1c1174 | 2015-12-14 22:39:30 +0200 | [diff] [blame] | 1002 | #endif |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 1003 | |
Laurent Pinchart | 7ef93b0 | 2015-12-14 22:39:33 +0200 | [diff] [blame] | 1004 | /* ----------------------------------------------------------------------------- |
| 1005 | * Power Management |
| 1006 | */ |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 1007 | |
Andy Gross | e78edba | 2012-12-19 14:53:37 -0600 | [diff] [blame] | 1008 | #ifdef CONFIG_PM |
| 1009 | /* re-pin objects in DMM in resume path: */ |
| 1010 | int omap_gem_resume(struct device *dev) |
| 1011 | { |
| 1012 | struct drm_device *drm_dev = dev_get_drvdata(dev); |
| 1013 | struct omap_drm_private *priv = drm_dev->dev_private; |
| 1014 | struct omap_gem_object *omap_obj; |
| 1015 | int ret = 0; |
| 1016 | |
| 1017 | list_for_each_entry(omap_obj, &priv->obj_list, mm_list) { |
| 1018 | if (omap_obj->block) { |
| 1019 | struct drm_gem_object *obj = &omap_obj->base; |
| 1020 | uint32_t npages = obj->size >> PAGE_SHIFT; |
| 1021 | WARN_ON(!omap_obj->pages); /* this can't happen */ |
| 1022 | ret = tiler_pin(omap_obj->block, |
| 1023 | omap_obj->pages, npages, |
| 1024 | omap_obj->roll, true); |
| 1025 | if (ret) { |
| 1026 | dev_err(dev, "could not repin: %d\n", ret); |
| 1027 | return ret; |
| 1028 | } |
| 1029 | } |
| 1030 | } |
| 1031 | |
| 1032 | return 0; |
| 1033 | } |
| 1034 | #endif |
| 1035 | |
Laurent Pinchart | 7ef93b0 | 2015-12-14 22:39:33 +0200 | [diff] [blame] | 1036 | /* ----------------------------------------------------------------------------- |
| 1037 | * DebugFS |
| 1038 | */ |
| 1039 | |
Rob Clark | f6b6036 | 2012-03-05 10:48:36 -0600 | [diff] [blame] | 1040 | #ifdef CONFIG_DEBUG_FS |
| 1041 | void omap_gem_describe(struct drm_gem_object *obj, struct seq_file *m) |
| 1042 | { |
Rob Clark | f6b6036 | 2012-03-05 10:48:36 -0600 | [diff] [blame] | 1043 | struct omap_gem_object *omap_obj = to_omap_bo(obj); |
David Herrmann | 0de2397 | 2013-07-24 21:07:52 +0200 | [diff] [blame] | 1044 | uint64_t off; |
Rob Clark | f6b6036 | 2012-03-05 10:48:36 -0600 | [diff] [blame] | 1045 | |
David Herrmann | 0de2397 | 2013-07-24 21:07:52 +0200 | [diff] [blame] | 1046 | off = drm_vma_node_start(&obj->vma_node); |
Rob Clark | f6b6036 | 2012-03-05 10:48:36 -0600 | [diff] [blame] | 1047 | |
Russell King | 2d31ca3 | 2014-07-12 10:53:41 +0100 | [diff] [blame] | 1048 | seq_printf(m, "%08x: %2d (%2d) %08llx %pad (%2d) %p %4d", |
Rob Clark | f6b6036 | 2012-03-05 10:48:36 -0600 | [diff] [blame] | 1049 | omap_obj->flags, obj->name, obj->refcount.refcount.counter, |
Russell King | 2d31ca3 | 2014-07-12 10:53:41 +0100 | [diff] [blame] | 1050 | off, &omap_obj->paddr, omap_obj->paddr_cnt, |
Rob Clark | f6b6036 | 2012-03-05 10:48:36 -0600 | [diff] [blame] | 1051 | omap_obj->vaddr, omap_obj->roll); |
| 1052 | |
| 1053 | if (omap_obj->flags & OMAP_BO_TILED) { |
| 1054 | seq_printf(m, " %dx%d", omap_obj->width, omap_obj->height); |
| 1055 | if (omap_obj->block) { |
| 1056 | struct tcm_area *area = &omap_obj->block->area; |
| 1057 | seq_printf(m, " (%dx%d, %dx%d)", |
| 1058 | area->p0.x, area->p0.y, |
| 1059 | area->p1.x, area->p1.y); |
| 1060 | } |
| 1061 | } else { |
| 1062 | seq_printf(m, " %d", obj->size); |
| 1063 | } |
| 1064 | |
| 1065 | seq_printf(m, "\n"); |
| 1066 | } |
| 1067 | |
| 1068 | void omap_gem_describe_objects(struct list_head *list, struct seq_file *m) |
| 1069 | { |
| 1070 | struct omap_gem_object *omap_obj; |
| 1071 | int count = 0; |
| 1072 | size_t size = 0; |
| 1073 | |
| 1074 | list_for_each_entry(omap_obj, list, mm_list) { |
| 1075 | struct drm_gem_object *obj = &omap_obj->base; |
| 1076 | seq_printf(m, " "); |
| 1077 | omap_gem_describe(obj, m); |
| 1078 | count++; |
| 1079 | size += obj->size; |
| 1080 | } |
| 1081 | |
| 1082 | seq_printf(m, "Total %d objects, %zu bytes\n", count, size); |
| 1083 | } |
| 1084 | #endif |
| 1085 | |
Laurent Pinchart | 7ef93b0 | 2015-12-14 22:39:33 +0200 | [diff] [blame] | 1086 | /* ----------------------------------------------------------------------------- |
| 1087 | * Buffer Synchronization |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 1088 | */ |
| 1089 | |
Laurent Pinchart | 7ef93b0 | 2015-12-14 22:39:33 +0200 | [diff] [blame] | 1090 | static DEFINE_SPINLOCK(sync_lock); |
| 1091 | |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 1092 | struct omap_gem_sync_waiter { |
| 1093 | struct list_head list; |
| 1094 | struct omap_gem_object *omap_obj; |
| 1095 | enum omap_gem_op op; |
| 1096 | uint32_t read_target, write_target; |
| 1097 | /* notify called w/ sync_lock held */ |
| 1098 | void (*notify)(void *arg); |
| 1099 | void *arg; |
| 1100 | }; |
| 1101 | |
| 1102 | /* list of omap_gem_sync_waiter.. the notify fxn gets called back when |
| 1103 | * the read and/or write target count is achieved which can call a user |
| 1104 | * callback (ex. to kick 3d and/or 2d), wakeup blocked task (prep for |
| 1105 | * cpu access), etc. |
| 1106 | */ |
| 1107 | static LIST_HEAD(waiters); |
| 1108 | |
| 1109 | static inline bool is_waiting(struct omap_gem_sync_waiter *waiter) |
| 1110 | { |
| 1111 | struct omap_gem_object *omap_obj = waiter->omap_obj; |
| 1112 | if ((waiter->op & OMAP_GEM_READ) && |
Archit Taneja | f2cff0f | 2014-04-11 12:53:31 +0530 | [diff] [blame] | 1113 | (omap_obj->sync->write_complete < waiter->write_target)) |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 1114 | return true; |
| 1115 | if ((waiter->op & OMAP_GEM_WRITE) && |
Archit Taneja | f2cff0f | 2014-04-11 12:53:31 +0530 | [diff] [blame] | 1116 | (omap_obj->sync->read_complete < waiter->read_target)) |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 1117 | return true; |
| 1118 | return false; |
| 1119 | } |
| 1120 | |
| 1121 | /* macro for sync debug.. */ |
| 1122 | #define SYNCDBG 0 |
| 1123 | #define SYNC(fmt, ...) do { if (SYNCDBG) \ |
| 1124 | printk(KERN_ERR "%s:%d: "fmt"\n", \ |
| 1125 | __func__, __LINE__, ##__VA_ARGS__); \ |
| 1126 | } while (0) |
| 1127 | |
| 1128 | |
| 1129 | static void sync_op_update(void) |
| 1130 | { |
| 1131 | struct omap_gem_sync_waiter *waiter, *n; |
| 1132 | list_for_each_entry_safe(waiter, n, &waiters, list) { |
| 1133 | if (!is_waiting(waiter)) { |
| 1134 | list_del(&waiter->list); |
| 1135 | SYNC("notify: %p", waiter); |
| 1136 | waiter->notify(waiter->arg); |
| 1137 | kfree(waiter); |
| 1138 | } |
| 1139 | } |
| 1140 | } |
| 1141 | |
| 1142 | static inline int sync_op(struct drm_gem_object *obj, |
| 1143 | enum omap_gem_op op, bool start) |
| 1144 | { |
| 1145 | struct omap_gem_object *omap_obj = to_omap_bo(obj); |
| 1146 | int ret = 0; |
| 1147 | |
| 1148 | spin_lock(&sync_lock); |
| 1149 | |
| 1150 | if (!omap_obj->sync) { |
| 1151 | omap_obj->sync = kzalloc(sizeof(*omap_obj->sync), GFP_ATOMIC); |
| 1152 | if (!omap_obj->sync) { |
| 1153 | ret = -ENOMEM; |
| 1154 | goto unlock; |
| 1155 | } |
| 1156 | } |
| 1157 | |
| 1158 | if (start) { |
| 1159 | if (op & OMAP_GEM_READ) |
| 1160 | omap_obj->sync->read_pending++; |
| 1161 | if (op & OMAP_GEM_WRITE) |
| 1162 | omap_obj->sync->write_pending++; |
| 1163 | } else { |
| 1164 | if (op & OMAP_GEM_READ) |
| 1165 | omap_obj->sync->read_complete++; |
| 1166 | if (op & OMAP_GEM_WRITE) |
| 1167 | omap_obj->sync->write_complete++; |
| 1168 | sync_op_update(); |
| 1169 | } |
| 1170 | |
| 1171 | unlock: |
| 1172 | spin_unlock(&sync_lock); |
| 1173 | |
| 1174 | return ret; |
| 1175 | } |
| 1176 | |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 1177 | /* mark the start of read and/or write operation */ |
| 1178 | int omap_gem_op_start(struct drm_gem_object *obj, enum omap_gem_op op) |
| 1179 | { |
| 1180 | return sync_op(obj, op, true); |
| 1181 | } |
| 1182 | |
| 1183 | int omap_gem_op_finish(struct drm_gem_object *obj, enum omap_gem_op op) |
| 1184 | { |
| 1185 | return sync_op(obj, op, false); |
| 1186 | } |
| 1187 | |
| 1188 | static DECLARE_WAIT_QUEUE_HEAD(sync_event); |
| 1189 | |
| 1190 | static void sync_notify(void *arg) |
| 1191 | { |
| 1192 | struct task_struct **waiter_task = arg; |
| 1193 | *waiter_task = NULL; |
| 1194 | wake_up_all(&sync_event); |
| 1195 | } |
| 1196 | |
| 1197 | int omap_gem_op_sync(struct drm_gem_object *obj, enum omap_gem_op op) |
| 1198 | { |
| 1199 | struct omap_gem_object *omap_obj = to_omap_bo(obj); |
| 1200 | int ret = 0; |
| 1201 | if (omap_obj->sync) { |
| 1202 | struct task_struct *waiter_task = current; |
| 1203 | struct omap_gem_sync_waiter *waiter = |
| 1204 | kzalloc(sizeof(*waiter), GFP_KERNEL); |
| 1205 | |
YAMANE Toshiaki | ae05303 | 2012-11-14 19:33:17 +0900 | [diff] [blame] | 1206 | if (!waiter) |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 1207 | return -ENOMEM; |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 1208 | |
| 1209 | waiter->omap_obj = omap_obj; |
| 1210 | waiter->op = op; |
| 1211 | waiter->read_target = omap_obj->sync->read_pending; |
| 1212 | waiter->write_target = omap_obj->sync->write_pending; |
| 1213 | waiter->notify = sync_notify; |
| 1214 | waiter->arg = &waiter_task; |
| 1215 | |
| 1216 | spin_lock(&sync_lock); |
| 1217 | if (is_waiting(waiter)) { |
| 1218 | SYNC("waited: %p", waiter); |
| 1219 | list_add_tail(&waiter->list, &waiters); |
| 1220 | spin_unlock(&sync_lock); |
| 1221 | ret = wait_event_interruptible(sync_event, |
| 1222 | (waiter_task == NULL)); |
| 1223 | spin_lock(&sync_lock); |
| 1224 | if (waiter_task) { |
| 1225 | SYNC("interrupted: %p", waiter); |
| 1226 | /* we were interrupted */ |
| 1227 | list_del(&waiter->list); |
| 1228 | waiter_task = NULL; |
| 1229 | } else { |
| 1230 | /* freed in sync_op_update() */ |
| 1231 | waiter = NULL; |
| 1232 | } |
| 1233 | } |
| 1234 | spin_unlock(&sync_lock); |
Fabian Frederick | d2c87e2 | 2014-07-04 21:17:15 +0200 | [diff] [blame] | 1235 | kfree(waiter); |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 1236 | } |
| 1237 | return ret; |
| 1238 | } |
| 1239 | |
| 1240 | /* call fxn(arg), either synchronously or asynchronously if the op |
| 1241 | * is currently blocked.. fxn() can be called from any context |
| 1242 | * |
| 1243 | * (TODO for now fxn is called back from whichever context calls |
Tomi Valkeinen | 3f50eff | 2016-01-27 10:58:43 +0200 | [diff] [blame] | 1244 | * omap_gem_op_finish().. but this could be better defined later |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 1245 | * if needed) |
| 1246 | * |
| 1247 | * TODO more code in common w/ _sync().. |
| 1248 | */ |
| 1249 | int omap_gem_op_async(struct drm_gem_object *obj, enum omap_gem_op op, |
| 1250 | void (*fxn)(void *arg), void *arg) |
| 1251 | { |
| 1252 | struct omap_gem_object *omap_obj = to_omap_bo(obj); |
| 1253 | if (omap_obj->sync) { |
| 1254 | struct omap_gem_sync_waiter *waiter = |
| 1255 | kzalloc(sizeof(*waiter), GFP_ATOMIC); |
| 1256 | |
YAMANE Toshiaki | ae05303 | 2012-11-14 19:33:17 +0900 | [diff] [blame] | 1257 | if (!waiter) |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 1258 | return -ENOMEM; |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 1259 | |
| 1260 | waiter->omap_obj = omap_obj; |
| 1261 | waiter->op = op; |
| 1262 | waiter->read_target = omap_obj->sync->read_pending; |
| 1263 | waiter->write_target = omap_obj->sync->write_pending; |
| 1264 | waiter->notify = fxn; |
| 1265 | waiter->arg = arg; |
| 1266 | |
| 1267 | spin_lock(&sync_lock); |
| 1268 | if (is_waiting(waiter)) { |
| 1269 | SYNC("waited: %p", waiter); |
| 1270 | list_add_tail(&waiter->list, &waiters); |
| 1271 | spin_unlock(&sync_lock); |
| 1272 | return 0; |
| 1273 | } |
| 1274 | |
| 1275 | spin_unlock(&sync_lock); |
Subhajit Paul | 15ec2ca | 2014-04-11 12:53:30 +0530 | [diff] [blame] | 1276 | |
| 1277 | kfree(waiter); |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 1278 | } |
| 1279 | |
| 1280 | /* no waiting.. */ |
| 1281 | fxn(arg); |
| 1282 | |
| 1283 | return 0; |
| 1284 | } |
| 1285 | |
Laurent Pinchart | 7ef93b0 | 2015-12-14 22:39:33 +0200 | [diff] [blame] | 1286 | /* ----------------------------------------------------------------------------- |
| 1287 | * Constructor & Destructor |
| 1288 | */ |
| 1289 | |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 1290 | void omap_gem_free_object(struct drm_gem_object *obj) |
| 1291 | { |
| 1292 | struct drm_device *dev = obj->dev; |
Tomi Valkeinen | 76c4055 | 2014-12-17 14:34:22 +0200 | [diff] [blame] | 1293 | struct omap_drm_private *priv = dev->dev_private; |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 1294 | struct omap_gem_object *omap_obj = to_omap_bo(obj); |
| 1295 | |
Rob Clark | f7f9f45 | 2011-12-05 19:19:22 -0600 | [diff] [blame] | 1296 | evict(obj); |
| 1297 | |
Rob Clark | f6b6036 | 2012-03-05 10:48:36 -0600 | [diff] [blame] | 1298 | WARN_ON(!mutex_is_locked(&dev->struct_mutex)); |
| 1299 | |
Tomi Valkeinen | 76c4055 | 2014-12-17 14:34:22 +0200 | [diff] [blame] | 1300 | spin_lock(&priv->list_lock); |
Rob Clark | f6b6036 | 2012-03-05 10:48:36 -0600 | [diff] [blame] | 1301 | list_del(&omap_obj->mm_list); |
Tomi Valkeinen | 76c4055 | 2014-12-17 14:34:22 +0200 | [diff] [blame] | 1302 | spin_unlock(&priv->list_lock); |
Rob Clark | f6b6036 | 2012-03-05 10:48:36 -0600 | [diff] [blame] | 1303 | |
Rob Clark | 9a0774e | 2012-01-16 12:51:17 -0600 | [diff] [blame] | 1304 | /* this means the object is still pinned.. which really should |
| 1305 | * not happen. I think.. |
| 1306 | */ |
| 1307 | WARN_ON(omap_obj->paddr_cnt > 0); |
| 1308 | |
Tomi Valkeinen | 3f50eff | 2016-01-27 10:58:43 +0200 | [diff] [blame] | 1309 | if (omap_obj->pages) { |
| 1310 | if (omap_obj->flags & OMAP_BO_MEM_DMABUF) |
| 1311 | kfree(omap_obj->pages); |
| 1312 | else |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 1313 | omap_gem_detach_pages(obj); |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 1314 | } |
| 1315 | |
Tomi Valkeinen | 3f50eff | 2016-01-27 10:58:43 +0200 | [diff] [blame] | 1316 | if (omap_obj->flags & OMAP_BO_MEM_DMA_API) { |
Linus Torvalds | 266c73b | 2016-03-21 13:48:00 -0700 | [diff] [blame] | 1317 | dma_free_wc(dev->dev, obj->size, omap_obj->vaddr, |
| 1318 | omap_obj->paddr); |
Tomi Valkeinen | 3f50eff | 2016-01-27 10:58:43 +0200 | [diff] [blame] | 1319 | } else if (omap_obj->vaddr) { |
| 1320 | vunmap(omap_obj->vaddr); |
| 1321 | } else if (obj->import_attach) { |
| 1322 | drm_prime_gem_destroy(obj, omap_obj->sgt); |
| 1323 | } |
| 1324 | |
| 1325 | kfree(omap_obj->sync); |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 1326 | |
| 1327 | drm_gem_object_release(obj); |
| 1328 | |
Laurent Pinchart | 00e9c7c | 2015-12-14 22:39:38 +0200 | [diff] [blame] | 1329 | kfree(omap_obj); |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 1330 | } |
| 1331 | |
| 1332 | /* GEM buffer object constructor */ |
| 1333 | struct drm_gem_object *omap_gem_new(struct drm_device *dev, |
| 1334 | union omap_gem_size gsize, uint32_t flags) |
| 1335 | { |
Rob Clark | a6a9182 | 2011-12-09 23:26:08 -0600 | [diff] [blame] | 1336 | struct omap_drm_private *priv = dev->dev_private; |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 1337 | struct omap_gem_object *omap_obj; |
Laurent Pinchart | 92b4b44 | 2015-12-14 22:39:41 +0200 | [diff] [blame] | 1338 | struct drm_gem_object *obj; |
David Herrmann | ab5a60c | 2014-05-25 12:45:39 +0200 | [diff] [blame] | 1339 | struct address_space *mapping; |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 1340 | size_t size; |
| 1341 | int ret; |
| 1342 | |
Laurent Pinchart | 9cba3b9 | 2015-12-14 22:39:43 +0200 | [diff] [blame] | 1343 | /* Validate the flags and compute the memory and cache flags. */ |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 1344 | if (flags & OMAP_BO_TILED) { |
Laurent Pinchart | f430274 | 2015-12-14 22:39:34 +0200 | [diff] [blame] | 1345 | if (!priv->usergart) { |
Rob Clark | f7f9f45 | 2011-12-05 19:19:22 -0600 | [diff] [blame] | 1346 | dev_err(dev->dev, "Tiled buffers require DMM\n"); |
Laurent Pinchart | 92b4b44 | 2015-12-14 22:39:41 +0200 | [diff] [blame] | 1347 | return NULL; |
Rob Clark | f7f9f45 | 2011-12-05 19:19:22 -0600 | [diff] [blame] | 1348 | } |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 1349 | |
Laurent Pinchart | 9cba3b9 | 2015-12-14 22:39:43 +0200 | [diff] [blame] | 1350 | /* |
| 1351 | * Tiled buffers are always shmem paged backed. When they are |
| 1352 | * scanned out, they are remapped into DMM/TILER. |
Rob Clark | f7f9f45 | 2011-12-05 19:19:22 -0600 | [diff] [blame] | 1353 | */ |
| 1354 | flags &= ~OMAP_BO_SCANOUT; |
Laurent Pinchart | 9cba3b9 | 2015-12-14 22:39:43 +0200 | [diff] [blame] | 1355 | flags |= OMAP_BO_MEM_SHMEM; |
Rob Clark | f7f9f45 | 2011-12-05 19:19:22 -0600 | [diff] [blame] | 1356 | |
Laurent Pinchart | 9cba3b9 | 2015-12-14 22:39:43 +0200 | [diff] [blame] | 1357 | /* |
| 1358 | * Currently don't allow cached buffers. There is some caching |
| 1359 | * stuff that needs to be handled better. |
Rob Clark | f7f9f45 | 2011-12-05 19:19:22 -0600 | [diff] [blame] | 1360 | */ |
Tomi Valkeinen | 7cb0d6c | 2014-09-25 19:24:29 +0000 | [diff] [blame] | 1361 | flags &= ~(OMAP_BO_CACHED|OMAP_BO_WC|OMAP_BO_UNCACHED); |
| 1362 | flags |= tiler_get_cpu_cache_flags(); |
Laurent Pinchart | 9cba3b9 | 2015-12-14 22:39:43 +0200 | [diff] [blame] | 1363 | } else if ((flags & OMAP_BO_SCANOUT) && !priv->has_dmm) { |
| 1364 | /* |
Laurent Pinchart | b22e669 | 2015-12-14 22:39:44 +0200 | [diff] [blame] | 1365 | * OMAP_BO_SCANOUT hints that the buffer doesn't need to be |
| 1366 | * tiled. However, to lower the pressure on memory allocation, |
| 1367 | * use contiguous memory only if no TILER is available. |
Laurent Pinchart | 9cba3b9 | 2015-12-14 22:39:43 +0200 | [diff] [blame] | 1368 | */ |
| 1369 | flags |= OMAP_BO_MEM_DMA_API; |
Tomi Valkeinen | 3f50eff | 2016-01-27 10:58:43 +0200 | [diff] [blame] | 1370 | } else if (!(flags & OMAP_BO_MEM_DMABUF)) { |
Laurent Pinchart | 9cba3b9 | 2015-12-14 22:39:43 +0200 | [diff] [blame] | 1371 | /* |
Tomi Valkeinen | 3f50eff | 2016-01-27 10:58:43 +0200 | [diff] [blame] | 1372 | * All other buffers not backed by dma_buf are shmem-backed. |
Laurent Pinchart | 9cba3b9 | 2015-12-14 22:39:43 +0200 | [diff] [blame] | 1373 | */ |
| 1374 | flags |= OMAP_BO_MEM_SHMEM; |
Rob Clark | f7f9f45 | 2011-12-05 19:19:22 -0600 | [diff] [blame] | 1375 | } |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 1376 | |
Laurent Pinchart | 9cba3b9 | 2015-12-14 22:39:43 +0200 | [diff] [blame] | 1377 | /* Allocate the initialize the OMAP GEM object. */ |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 1378 | omap_obj = kzalloc(sizeof(*omap_obj), GFP_KERNEL); |
Joe Perches | 78110bb | 2013-02-11 09:41:29 -0800 | [diff] [blame] | 1379 | if (!omap_obj) |
Tomi Valkeinen | a903e3b | 2015-03-17 15:31:11 +0200 | [diff] [blame] | 1380 | return NULL; |
Rob Clark | f6b6036 | 2012-03-05 10:48:36 -0600 | [diff] [blame] | 1381 | |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 1382 | obj = &omap_obj->base; |
Laurent Pinchart | 9cba3b9 | 2015-12-14 22:39:43 +0200 | [diff] [blame] | 1383 | omap_obj->flags = flags; |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 1384 | |
Laurent Pinchart | 9cba3b9 | 2015-12-14 22:39:43 +0200 | [diff] [blame] | 1385 | if (flags & OMAP_BO_TILED) { |
| 1386 | /* |
| 1387 | * For tiled buffers align dimensions to slot boundaries and |
| 1388 | * calculate size based on aligned dimensions. |
Rob Clark | a6a9182 | 2011-12-09 23:26:08 -0600 | [diff] [blame] | 1389 | */ |
Laurent Pinchart | 9cba3b9 | 2015-12-14 22:39:43 +0200 | [diff] [blame] | 1390 | tiler_align(gem2fmt(flags), &gsize.tiled.width, |
| 1391 | &gsize.tiled.height); |
YAMANE Toshiaki | ae05303 | 2012-11-14 19:33:17 +0900 | [diff] [blame] | 1392 | |
Laurent Pinchart | 9cba3b9 | 2015-12-14 22:39:43 +0200 | [diff] [blame] | 1393 | size = tiler_size(gem2fmt(flags), gsize.tiled.width, |
| 1394 | gsize.tiled.height); |
Tomi Valkeinen | a903e3b | 2015-03-17 15:31:11 +0200 | [diff] [blame] | 1395 | |
Laurent Pinchart | 9cba3b9 | 2015-12-14 22:39:43 +0200 | [diff] [blame] | 1396 | omap_obj->width = gsize.tiled.width; |
| 1397 | omap_obj->height = gsize.tiled.height; |
| 1398 | } else { |
| 1399 | size = PAGE_ALIGN(gsize.bytes); |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 1400 | } |
| 1401 | |
Laurent Pinchart | c2eb77f | 2016-03-02 12:51:19 +0200 | [diff] [blame] | 1402 | /* Initialize the GEM object. */ |
| 1403 | if (!(flags & OMAP_BO_MEM_SHMEM)) { |
| 1404 | drm_gem_private_object_init(dev, obj, size); |
| 1405 | } else { |
| 1406 | ret = drm_gem_object_init(dev, obj, size); |
| 1407 | if (ret) |
| 1408 | goto err_free; |
| 1409 | |
| 1410 | mapping = file_inode(obj->filp)->i_mapping; |
| 1411 | mapping_set_gfp_mask(mapping, GFP_USER | __GFP_DMA32); |
| 1412 | } |
Tomi Valkeinen | a903e3b | 2015-03-17 15:31:11 +0200 | [diff] [blame] | 1413 | |
Laurent Pinchart | 9cba3b9 | 2015-12-14 22:39:43 +0200 | [diff] [blame] | 1414 | /* Allocate memory if needed. */ |
| 1415 | if (flags & OMAP_BO_MEM_DMA_API) { |
Linus Torvalds | 266c73b | 2016-03-21 13:48:00 -0700 | [diff] [blame] | 1416 | omap_obj->vaddr = dma_alloc_wc(dev->dev, size, |
| 1417 | &omap_obj->paddr, |
| 1418 | GFP_KERNEL); |
Laurent Pinchart | 9cba3b9 | 2015-12-14 22:39:43 +0200 | [diff] [blame] | 1419 | if (!omap_obj->vaddr) |
Laurent Pinchart | c2eb77f | 2016-03-02 12:51:19 +0200 | [diff] [blame] | 1420 | goto err_release; |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 1421 | } |
| 1422 | |
| 1423 | spin_lock(&priv->list_lock); |
| 1424 | list_add(&omap_obj->mm_list, &priv->obj_list); |
Rob Clark | f7f9f45 | 2011-12-05 19:19:22 -0600 | [diff] [blame] | 1425 | spin_unlock(&priv->list_lock); |
| 1426 | |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 1427 | return obj; |
| 1428 | |
Laurent Pinchart | c2eb77f | 2016-03-02 12:51:19 +0200 | [diff] [blame] | 1429 | err_release: |
| 1430 | drm_gem_object_release(obj); |
| 1431 | err_free: |
| 1432 | kfree(omap_obj); |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 1433 | return NULL; |
| 1434 | } |
Rob Clark | f7f9f45 | 2011-12-05 19:19:22 -0600 | [diff] [blame] | 1435 | |
Laurent Pinchart | b22e669 | 2015-12-14 22:39:44 +0200 | [diff] [blame] | 1436 | struct drm_gem_object *omap_gem_new_dmabuf(struct drm_device *dev, size_t size, |
| 1437 | struct sg_table *sgt) |
| 1438 | { |
| 1439 | struct omap_drm_private *priv = dev->dev_private; |
| 1440 | struct omap_gem_object *omap_obj; |
| 1441 | struct drm_gem_object *obj; |
| 1442 | union omap_gem_size gsize; |
| 1443 | |
| 1444 | /* Without a DMM only physically contiguous buffers can be supported. */ |
| 1445 | if (sgt->orig_nents != 1 && !priv->has_dmm) |
| 1446 | return ERR_PTR(-EINVAL); |
| 1447 | |
| 1448 | mutex_lock(&dev->struct_mutex); |
| 1449 | |
| 1450 | gsize.bytes = PAGE_ALIGN(size); |
| 1451 | obj = omap_gem_new(dev, gsize, OMAP_BO_MEM_DMABUF | OMAP_BO_WC); |
| 1452 | if (!obj) { |
| 1453 | obj = ERR_PTR(-ENOMEM); |
| 1454 | goto done; |
| 1455 | } |
| 1456 | |
| 1457 | omap_obj = to_omap_bo(obj); |
| 1458 | omap_obj->sgt = sgt; |
| 1459 | |
| 1460 | if (sgt->orig_nents == 1) { |
| 1461 | omap_obj->paddr = sg_dma_address(sgt->sgl); |
| 1462 | } else { |
| 1463 | /* Create pages list from sgt */ |
| 1464 | struct sg_page_iter iter; |
| 1465 | struct page **pages; |
| 1466 | unsigned int npages; |
| 1467 | unsigned int i = 0; |
| 1468 | |
| 1469 | npages = DIV_ROUND_UP(size, PAGE_SIZE); |
| 1470 | pages = kcalloc(npages, sizeof(*pages), GFP_KERNEL); |
| 1471 | if (!pages) { |
| 1472 | omap_gem_free_object(obj); |
| 1473 | obj = ERR_PTR(-ENOMEM); |
| 1474 | goto done; |
| 1475 | } |
| 1476 | |
| 1477 | omap_obj->pages = pages; |
| 1478 | |
| 1479 | for_each_sg_page(sgt->sgl, &iter, sgt->orig_nents, 0) { |
| 1480 | pages[i++] = sg_page_iter_page(&iter); |
| 1481 | if (i > npages) |
| 1482 | break; |
| 1483 | } |
| 1484 | |
| 1485 | if (WARN_ON(i != npages)) { |
| 1486 | omap_gem_free_object(obj); |
| 1487 | obj = ERR_PTR(-ENOMEM); |
| 1488 | goto done; |
| 1489 | } |
| 1490 | } |
| 1491 | |
| 1492 | done: |
| 1493 | mutex_unlock(&dev->struct_mutex); |
| 1494 | return obj; |
| 1495 | } |
| 1496 | |
Laurent Pinchart | 7ef93b0 | 2015-12-14 22:39:33 +0200 | [diff] [blame] | 1497 | /* convenience method to construct a GEM buffer object, and userspace handle */ |
| 1498 | int omap_gem_new_handle(struct drm_device *dev, struct drm_file *file, |
| 1499 | union omap_gem_size gsize, uint32_t flags, uint32_t *handle) |
| 1500 | { |
| 1501 | struct drm_gem_object *obj; |
| 1502 | int ret; |
| 1503 | |
| 1504 | obj = omap_gem_new(dev, gsize, flags); |
| 1505 | if (!obj) |
| 1506 | return -ENOMEM; |
| 1507 | |
| 1508 | ret = drm_gem_handle_create(file, obj, handle); |
| 1509 | if (ret) { |
Laurent Pinchart | 74128a2 | 2015-12-14 22:39:39 +0200 | [diff] [blame] | 1510 | omap_gem_free_object(obj); |
Laurent Pinchart | 7ef93b0 | 2015-12-14 22:39:33 +0200 | [diff] [blame] | 1511 | return ret; |
| 1512 | } |
| 1513 | |
| 1514 | /* drop reference from allocate - handle holds it now */ |
| 1515 | drm_gem_object_unreference_unlocked(obj); |
| 1516 | |
| 1517 | return 0; |
| 1518 | } |
| 1519 | |
| 1520 | /* ----------------------------------------------------------------------------- |
| 1521 | * Init & Cleanup |
| 1522 | */ |
| 1523 | |
| 1524 | /* If DMM is used, we need to set some stuff up.. */ |
Rob Clark | f7f9f45 | 2011-12-05 19:19:22 -0600 | [diff] [blame] | 1525 | void omap_gem_init(struct drm_device *dev) |
| 1526 | { |
Rob Clark | a6a9182 | 2011-12-09 23:26:08 -0600 | [diff] [blame] | 1527 | struct omap_drm_private *priv = dev->dev_private; |
Laurent Pinchart | f430274 | 2015-12-14 22:39:34 +0200 | [diff] [blame] | 1528 | struct omap_drm_usergart *usergart; |
Rob Clark | f7f9f45 | 2011-12-05 19:19:22 -0600 | [diff] [blame] | 1529 | const enum tiler_fmt fmts[] = { |
| 1530 | TILFMT_8BIT, TILFMT_16BIT, TILFMT_32BIT |
| 1531 | }; |
Andy Gross | 5c13779 | 2012-03-05 10:48:39 -0600 | [diff] [blame] | 1532 | int i, j; |
Rob Clark | f7f9f45 | 2011-12-05 19:19:22 -0600 | [diff] [blame] | 1533 | |
Andy Gross | e5e4e9b | 2012-10-17 00:30:03 -0500 | [diff] [blame] | 1534 | if (!dmm_is_available()) { |
Rob Clark | f7f9f45 | 2011-12-05 19:19:22 -0600 | [diff] [blame] | 1535 | /* DMM only supported on OMAP4 and later, so this isn't fatal */ |
Andy Gross | 5c13779 | 2012-03-05 10:48:39 -0600 | [diff] [blame] | 1536 | dev_warn(dev->dev, "DMM not available, disable DMM support\n"); |
Rob Clark | f7f9f45 | 2011-12-05 19:19:22 -0600 | [diff] [blame] | 1537 | return; |
| 1538 | } |
| 1539 | |
Joe Perches | 78110bb | 2013-02-11 09:41:29 -0800 | [diff] [blame] | 1540 | usergart = kcalloc(3, sizeof(*usergart), GFP_KERNEL); |
| 1541 | if (!usergart) |
Rob Clark | b369839 | 2011-12-09 23:26:06 -0600 | [diff] [blame] | 1542 | return; |
Rob Clark | f7f9f45 | 2011-12-05 19:19:22 -0600 | [diff] [blame] | 1543 | |
| 1544 | /* reserve 4k aligned/wide regions for userspace mappings: */ |
| 1545 | for (i = 0; i < ARRAY_SIZE(fmts); i++) { |
| 1546 | uint16_t h = 1, w = PAGE_SIZE >> i; |
| 1547 | tiler_align(fmts[i], &w, &h); |
| 1548 | /* note: since each region is 1 4kb page wide, and minimum |
| 1549 | * number of rows, the height ends up being the same as the |
| 1550 | * # of pages in the region |
| 1551 | */ |
| 1552 | usergart[i].height = h; |
| 1553 | usergart[i].height_shift = ilog2(h); |
Rob Clark | 3c810c6 | 2012-08-15 15:18:01 -0500 | [diff] [blame] | 1554 | usergart[i].stride_pfn = tiler_stride(fmts[i], 0) >> PAGE_SHIFT; |
Rob Clark | f7f9f45 | 2011-12-05 19:19:22 -0600 | [diff] [blame] | 1555 | usergart[i].slot_shift = ilog2((PAGE_SIZE / h) >> i); |
| 1556 | for (j = 0; j < NUM_USERGART_ENTRIES; j++) { |
Laurent Pinchart | f430274 | 2015-12-14 22:39:34 +0200 | [diff] [blame] | 1557 | struct omap_drm_usergart_entry *entry; |
| 1558 | struct tiler_block *block; |
| 1559 | |
| 1560 | entry = &usergart[i].entry[j]; |
| 1561 | block = tiler_reserve_2d(fmts[i], w, h, PAGE_SIZE); |
Rob Clark | f7f9f45 | 2011-12-05 19:19:22 -0600 | [diff] [blame] | 1562 | if (IS_ERR(block)) { |
| 1563 | dev_err(dev->dev, |
| 1564 | "reserve failed: %d, %d, %ld\n", |
| 1565 | i, j, PTR_ERR(block)); |
| 1566 | return; |
| 1567 | } |
| 1568 | entry->paddr = tiler_ssptr(block); |
| 1569 | entry->block = block; |
| 1570 | |
Russell King | 2d31ca3 | 2014-07-12 10:53:41 +0100 | [diff] [blame] | 1571 | DBG("%d:%d: %dx%d: paddr=%pad stride=%d", i, j, w, h, |
| 1572 | &entry->paddr, |
Rob Clark | f7f9f45 | 2011-12-05 19:19:22 -0600 | [diff] [blame] | 1573 | usergart[i].stride_pfn << PAGE_SHIFT); |
| 1574 | } |
| 1575 | } |
Rob Clark | a6a9182 | 2011-12-09 23:26:08 -0600 | [diff] [blame] | 1576 | |
Laurent Pinchart | f430274 | 2015-12-14 22:39:34 +0200 | [diff] [blame] | 1577 | priv->usergart = usergart; |
Rob Clark | a6a9182 | 2011-12-09 23:26:08 -0600 | [diff] [blame] | 1578 | priv->has_dmm = true; |
Rob Clark | f7f9f45 | 2011-12-05 19:19:22 -0600 | [diff] [blame] | 1579 | } |
| 1580 | |
| 1581 | void omap_gem_deinit(struct drm_device *dev) |
| 1582 | { |
Laurent Pinchart | f430274 | 2015-12-14 22:39:34 +0200 | [diff] [blame] | 1583 | struct omap_drm_private *priv = dev->dev_private; |
| 1584 | |
Rob Clark | f7f9f45 | 2011-12-05 19:19:22 -0600 | [diff] [blame] | 1585 | /* I believe we can rely on there being no more outstanding GEM |
| 1586 | * objects which could depend on usergart/dmm at this point. |
| 1587 | */ |
Laurent Pinchart | f430274 | 2015-12-14 22:39:34 +0200 | [diff] [blame] | 1588 | kfree(priv->usergart); |
Rob Clark | f7f9f45 | 2011-12-05 19:19:22 -0600 | [diff] [blame] | 1589 | } |