Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 Red Hat |
| 3 | * |
| 4 | * This file is subject to the terms and conditions of the GNU General Public |
| 5 | * License v2. See the file COPYING in the main directory of this archive for |
| 6 | * more details. |
| 7 | */ |
| 8 | |
David Howells | 760285e | 2012-10-02 18:01:07 +0100 | [diff] [blame] | 9 | #include <drm/drmP.h> |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 10 | #include "udl_drv.h" |
| 11 | #include <linux/shmem_fs.h> |
Dave Airlie | 96503f5 | 2011-12-21 11:23:44 +0000 | [diff] [blame] | 12 | #include <linux/dma-buf.h> |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 13 | |
| 14 | struct udl_gem_object *udl_gem_alloc_object(struct drm_device *dev, |
| 15 | size_t size) |
| 16 | { |
| 17 | struct udl_gem_object *obj; |
| 18 | |
| 19 | obj = kzalloc(sizeof(*obj), GFP_KERNEL); |
| 20 | if (obj == NULL) |
| 21 | return NULL; |
| 22 | |
| 23 | if (drm_gem_object_init(dev, &obj->base, size) != 0) { |
| 24 | kfree(obj); |
| 25 | return NULL; |
| 26 | } |
| 27 | |
Haixia Shi | 09a58da | 2014-11-12 18:33:52 -0800 | [diff] [blame] | 28 | obj->flags = UDL_BO_CACHEABLE; |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 29 | return obj; |
| 30 | } |
| 31 | |
| 32 | static int |
| 33 | udl_gem_create(struct drm_file *file, |
| 34 | struct drm_device *dev, |
| 35 | uint64_t size, |
| 36 | uint32_t *handle_p) |
| 37 | { |
| 38 | struct udl_gem_object *obj; |
| 39 | int ret; |
| 40 | u32 handle; |
| 41 | |
| 42 | size = roundup(size, PAGE_SIZE); |
| 43 | |
| 44 | obj = udl_gem_alloc_object(dev, size); |
| 45 | if (obj == NULL) |
| 46 | return -ENOMEM; |
| 47 | |
| 48 | ret = drm_gem_handle_create(file, &obj->base, &handle); |
| 49 | if (ret) { |
| 50 | drm_gem_object_release(&obj->base); |
| 51 | kfree(obj); |
| 52 | return ret; |
| 53 | } |
| 54 | |
Cihangir Akturk | 823ee8b | 2017-08-11 15:33:09 +0300 | [diff] [blame] | 55 | drm_gem_object_put_unlocked(&obj->base); |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 56 | *handle_p = handle; |
| 57 | return 0; |
| 58 | } |
| 59 | |
Haixia Shi | 09a58da | 2014-11-12 18:33:52 -0800 | [diff] [blame] | 60 | static void update_vm_cache_attr(struct udl_gem_object *obj, |
| 61 | struct vm_area_struct *vma) |
| 62 | { |
| 63 | DRM_DEBUG_KMS("flags = 0x%x\n", obj->flags); |
| 64 | |
| 65 | /* non-cacheable as default. */ |
| 66 | if (obj->flags & UDL_BO_CACHEABLE) { |
| 67 | vma->vm_page_prot = vm_get_page_prot(vma->vm_flags); |
| 68 | } else if (obj->flags & UDL_BO_WC) { |
| 69 | vma->vm_page_prot = |
| 70 | pgprot_writecombine(vm_get_page_prot(vma->vm_flags)); |
| 71 | } else { |
| 72 | vma->vm_page_prot = |
| 73 | pgprot_noncached(vm_get_page_prot(vma->vm_flags)); |
| 74 | } |
| 75 | } |
| 76 | |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 77 | int udl_dumb_create(struct drm_file *file, |
| 78 | struct drm_device *dev, |
| 79 | struct drm_mode_create_dumb *args) |
| 80 | { |
David Herrmann | 2b932d8 | 2014-01-20 19:54:18 +0100 | [diff] [blame] | 81 | args->pitch = args->width * DIV_ROUND_UP(args->bpp, 8); |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 82 | args->size = args->pitch * args->height; |
| 83 | return udl_gem_create(file, dev, |
| 84 | args->size, &args->handle); |
| 85 | } |
| 86 | |
Konstantin Khlebnikov | fa9e855 | 2012-03-31 13:29:25 +0400 | [diff] [blame] | 87 | int udl_drm_gem_mmap(struct file *filp, struct vm_area_struct *vma) |
| 88 | { |
| 89 | int ret; |
| 90 | |
| 91 | ret = drm_gem_mmap(filp, vma); |
| 92 | if (ret) |
| 93 | return ret; |
| 94 | |
| 95 | vma->vm_flags &= ~VM_PFNMAP; |
| 96 | vma->vm_flags |= VM_MIXEDMAP; |
| 97 | |
Haixia Shi | 09a58da | 2014-11-12 18:33:52 -0800 | [diff] [blame] | 98 | update_vm_cache_attr(to_udl_bo(vma->vm_private_data), vma); |
| 99 | |
Konstantin Khlebnikov | fa9e855 | 2012-03-31 13:29:25 +0400 | [diff] [blame] | 100 | return ret; |
| 101 | } |
| 102 | |
Souptick Joarder | 3c5f134 | 2018-04-25 10:29:22 +0530 | [diff] [blame] | 103 | vm_fault_t udl_gem_fault(struct vm_fault *vmf) |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 104 | { |
Dave Jiang | 11bac80 | 2017-02-24 14:56:41 -0800 | [diff] [blame] | 105 | struct vm_area_struct *vma = vmf->vma; |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 106 | struct udl_gem_object *obj = to_udl_bo(vma->vm_private_data); |
| 107 | struct page *page; |
| 108 | unsigned int page_offset; |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 109 | |
Jan Kara | 1a29d85 | 2016-12-14 15:07:01 -0800 | [diff] [blame] | 110 | page_offset = (vmf->address - vma->vm_start) >> PAGE_SHIFT; |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 111 | |
| 112 | if (!obj->pages) |
| 113 | return VM_FAULT_SIGBUS; |
| 114 | |
| 115 | page = obj->pages[page_offset]; |
Souptick Joarder | 3c5f134 | 2018-04-25 10:29:22 +0530 | [diff] [blame] | 116 | return vmf_insert_page(vma, vmf->address, page); |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 117 | } |
| 118 | |
Haixia Shi | 4bc158e | 2014-11-25 12:04:02 -0800 | [diff] [blame] | 119 | int udl_gem_get_pages(struct udl_gem_object *obj) |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 120 | { |
Rob Clark | 5dc9e1e | 2013-08-07 13:41:26 -0400 | [diff] [blame] | 121 | struct page **pages; |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 122 | |
| 123 | if (obj->pages) |
| 124 | return 0; |
| 125 | |
David Herrmann | 0cdbe8a | 2014-05-25 12:59:47 +0200 | [diff] [blame] | 126 | pages = drm_gem_get_pages(&obj->base); |
Rob Clark | 5dc9e1e | 2013-08-07 13:41:26 -0400 | [diff] [blame] | 127 | if (IS_ERR(pages)) |
| 128 | return PTR_ERR(pages); |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 129 | |
Rob Clark | 5dc9e1e | 2013-08-07 13:41:26 -0400 | [diff] [blame] | 130 | obj->pages = pages; |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 131 | |
| 132 | return 0; |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 133 | } |
| 134 | |
Haixia Shi | 4bc158e | 2014-11-25 12:04:02 -0800 | [diff] [blame] | 135 | void udl_gem_put_pages(struct udl_gem_object *obj) |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 136 | { |
Rob Clark | 1d507b3 | 2013-12-04 08:45:43 -0500 | [diff] [blame] | 137 | if (obj->base.import_attach) { |
Michal Hocko | 2098105 | 2017-05-17 14:23:12 +0200 | [diff] [blame] | 138 | kvfree(obj->pages); |
Rob Clark | 1d507b3 | 2013-12-04 08:45:43 -0500 | [diff] [blame] | 139 | obj->pages = NULL; |
| 140 | return; |
| 141 | } |
| 142 | |
Rob Clark | 5dc9e1e | 2013-08-07 13:41:26 -0400 | [diff] [blame] | 143 | drm_gem_put_pages(&obj->base, obj->pages, false, false); |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 144 | obj->pages = NULL; |
| 145 | } |
| 146 | |
| 147 | int udl_gem_vmap(struct udl_gem_object *obj) |
| 148 | { |
| 149 | int page_count = obj->base.size / PAGE_SIZE; |
| 150 | int ret; |
| 151 | |
Dave Airlie | e8aa1d1 | 2012-03-26 14:36:56 +0100 | [diff] [blame] | 152 | if (obj->base.import_attach) { |
Dave Airlie | e8aa1d1 | 2012-03-26 14:36:56 +0100 | [diff] [blame] | 153 | obj->vmapping = dma_buf_vmap(obj->base.import_attach->dmabuf); |
| 154 | if (!obj->vmapping) |
| 155 | return -ENOMEM; |
| 156 | return 0; |
| 157 | } |
| 158 | |
David Herrmann | 0cdbe8a | 2014-05-25 12:59:47 +0200 | [diff] [blame] | 159 | ret = udl_gem_get_pages(obj); |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 160 | if (ret) |
| 161 | return ret; |
| 162 | |
| 163 | obj->vmapping = vmap(obj->pages, page_count, 0, PAGE_KERNEL); |
| 164 | if (!obj->vmapping) |
| 165 | return -ENOMEM; |
| 166 | return 0; |
| 167 | } |
| 168 | |
| 169 | void udl_gem_vunmap(struct udl_gem_object *obj) |
| 170 | { |
Dave Airlie | e8aa1d1 | 2012-03-26 14:36:56 +0100 | [diff] [blame] | 171 | if (obj->base.import_attach) { |
| 172 | dma_buf_vunmap(obj->base.import_attach->dmabuf, obj->vmapping); |
Dave Airlie | e8aa1d1 | 2012-03-26 14:36:56 +0100 | [diff] [blame] | 173 | return; |
| 174 | } |
| 175 | |
Markus Elfring | e9196d2 | 2014-11-19 17:33:32 +0100 | [diff] [blame] | 176 | vunmap(obj->vmapping); |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 177 | |
| 178 | udl_gem_put_pages(obj); |
| 179 | } |
| 180 | |
| 181 | void udl_gem_free_object(struct drm_gem_object *gem_obj) |
| 182 | { |
| 183 | struct udl_gem_object *obj = to_udl_bo(gem_obj); |
| 184 | |
| 185 | if (obj->vmapping) |
| 186 | udl_gem_vunmap(obj); |
| 187 | |
Dave Airlie | 347cf10 | 2014-03-25 14:38:44 +1000 | [diff] [blame] | 188 | if (gem_obj->import_attach) { |
Dave Airlie | e8aa1d1 | 2012-03-26 14:36:56 +0100 | [diff] [blame] | 189 | drm_prime_gem_destroy(gem_obj, obj->sg); |
Dave Airlie | 347cf10 | 2014-03-25 14:38:44 +1000 | [diff] [blame] | 190 | put_device(gem_obj->dev->dev); |
| 191 | } |
Dave Airlie | e8aa1d1 | 2012-03-26 14:36:56 +0100 | [diff] [blame] | 192 | |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 193 | if (obj->pages) |
| 194 | udl_gem_put_pages(obj); |
| 195 | |
David Herrmann | 0de2397 | 2013-07-24 21:07:52 +0200 | [diff] [blame] | 196 | drm_gem_free_mmap_offset(gem_obj); |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | /* the dumb interface doesn't work with the GEM straight MMAP |
| 200 | interface, it expects to do MMAP on the drm fd, like normal */ |
| 201 | int udl_gem_mmap(struct drm_file *file, struct drm_device *dev, |
| 202 | uint32_t handle, uint64_t *offset) |
| 203 | { |
| 204 | struct udl_gem_object *gobj; |
| 205 | struct drm_gem_object *obj; |
Daniel Vetter | ae358da | 2018-03-27 10:23:54 +0200 | [diff] [blame] | 206 | struct udl_device *udl = dev->dev_private; |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 207 | int ret = 0; |
| 208 | |
Daniel Vetter | ae358da | 2018-03-27 10:23:54 +0200 | [diff] [blame] | 209 | mutex_lock(&udl->gem_lock); |
Chris Wilson | a8ad0bd | 2016-05-09 11:04:54 +0100 | [diff] [blame] | 210 | obj = drm_gem_object_lookup(file, handle); |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 211 | if (obj == NULL) { |
| 212 | ret = -ENOENT; |
| 213 | goto unlock; |
| 214 | } |
| 215 | gobj = to_udl_bo(obj); |
| 216 | |
David Herrmann | 0cdbe8a | 2014-05-25 12:59:47 +0200 | [diff] [blame] | 217 | ret = udl_gem_get_pages(gobj); |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 218 | if (ret) |
Dan Carpenter | ace281e | 2012-03-22 09:30:56 +0300 | [diff] [blame] | 219 | goto out; |
David Herrmann | 0de2397 | 2013-07-24 21:07:52 +0200 | [diff] [blame] | 220 | ret = drm_gem_create_mmap_offset(obj); |
| 221 | if (ret) |
| 222 | goto out; |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 223 | |
David Herrmann | 0de2397 | 2013-07-24 21:07:52 +0200 | [diff] [blame] | 224 | *offset = drm_vma_node_offset_addr(&gobj->base.vma_node); |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 225 | |
| 226 | out: |
Dave Airlie | 8f3b487 | 2019-03-15 11:37:20 +1000 | [diff] [blame] | 227 | drm_gem_object_put_unlocked(&gobj->base); |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 228 | unlock: |
Daniel Vetter | ae358da | 2018-03-27 10:23:54 +0200 | [diff] [blame] | 229 | mutex_unlock(&udl->gem_lock); |
Dave Airlie | 5320918 | 2010-12-15 07:14:24 +1000 | [diff] [blame] | 230 | return ret; |
| 231 | } |