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