blob: a285340039fe2370e05333f11a860f88e29dee39 [file] [log] [blame]
Eric Anholt6a9eb082008-06-03 09:27:37 -07001/*
2 * Copyright © 2007 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 *
26 */
27
Eric Anholt368b3922008-09-10 13:54:34 -070028#ifdef HAVE_CONFIG_H
29#include "config.h"
30#endif
31
Eric Anholt6a9eb082008-06-03 09:27:37 -070032#include <string.h>
33#include <stdlib.h>
Eric Anholt738e36a2008-09-05 10:35:32 +010034#include <stdint.h>
Eric Anholt6a9eb082008-06-03 09:27:37 -070035#include <assert.h>
Eric Anholt738e36a2008-09-05 10:35:32 +010036#include <errno.h>
37#include <drm.h>
38#include <i915_drm.h>
Chris Wilson9d776032011-06-04 12:47:19 +010039#include <pciaccess.h>
Emil Velikov42465fe2015-04-05 15:51:59 +010040#include "libdrm_macros.h"
Eric Anholt738e36a2008-09-05 10:35:32 +010041#include "intel_bufmgr.h"
42#include "intel_bufmgr_priv.h"
Chris Wilson9d776032011-06-04 12:47:19 +010043#include "xf86drm.h"
Eric Anholt6a9eb082008-06-03 09:27:37 -070044
Eric Anholt4b982642008-10-30 09:33:07 -070045/** @file intel_bufmgr.c
Eric Anholt6a9eb082008-06-03 09:27:37 -070046 *
47 * Convenience functions for buffer management methods.
48 */
49
Emil Velikov0f8da822015-03-31 22:32:11 +010050drm_intel_bo *
Maarten Lankhorst07fead42014-07-31 15:07:27 +020051drm_intel_bo_alloc(drm_intel_bufmgr *bufmgr, const char *name,
52 unsigned long size, unsigned int alignment)
Eric Anholt6a9eb082008-06-03 09:27:37 -070053{
Eric Anholtd70d6052009-10-06 12:40:42 -070054 return bufmgr->bo_alloc(bufmgr, name, size, alignment);
Eric Anholt6a9eb082008-06-03 09:27:37 -070055}
56
Emil Velikov0f8da822015-03-31 22:32:11 +010057drm_intel_bo *
Maarten Lankhorst07fead42014-07-31 15:07:27 +020058drm_intel_bo_alloc_for_render(drm_intel_bufmgr *bufmgr, const char *name,
59 unsigned long size, unsigned int alignment)
Eric Anholt72abe982009-02-18 13:06:35 -080060{
Eric Anholtd70d6052009-10-06 12:40:42 -070061 return bufmgr->bo_alloc_for_render(bufmgr, name, size, alignment);
Eric Anholt72abe982009-02-18 13:06:35 -080062}
63
Emil Velikov0f8da822015-03-31 22:32:11 +010064drm_intel_bo *
Tvrtko Ursulinae8edc72014-06-19 15:52:03 +010065drm_intel_bo_alloc_userptr(drm_intel_bufmgr *bufmgr,
66 const char *name, void *addr,
67 uint32_t tiling_mode,
68 uint32_t stride,
69 unsigned long size,
70 unsigned long flags)
71{
72 if (bufmgr->bo_alloc_userptr)
73 return bufmgr->bo_alloc_userptr(bufmgr, name, addr, tiling_mode,
74 stride, size, flags);
75 return NULL;
76}
77
Emil Velikov0f8da822015-03-31 22:32:11 +010078drm_intel_bo *
Jesse Barnes3a7dfcd2009-10-06 14:34:06 -070079drm_intel_bo_alloc_tiled(drm_intel_bufmgr *bufmgr, const char *name,
80 int x, int y, int cpp, uint32_t *tiling_mode,
81 unsigned long *pitch, unsigned long flags)
82{
83 return bufmgr->bo_alloc_tiled(bufmgr, name, x, y, cpp,
84 tiling_mode, pitch, flags);
85}
86
Emil Velikov0f8da822015-03-31 22:32:11 +010087void
Maarten Lankhorst07fead42014-07-31 15:07:27 +020088drm_intel_bo_reference(drm_intel_bo *bo)
Eric Anholt6a9eb082008-06-03 09:27:37 -070089{
Eric Anholtd70d6052009-10-06 12:40:42 -070090 bo->bufmgr->bo_reference(bo);
Eric Anholt6a9eb082008-06-03 09:27:37 -070091}
92
Emil Velikov0f8da822015-03-31 22:32:11 +010093void
Maarten Lankhorst07fead42014-07-31 15:07:27 +020094drm_intel_bo_unreference(drm_intel_bo *bo)
Eric Anholt6a9eb082008-06-03 09:27:37 -070095{
Eric Anholtd70d6052009-10-06 12:40:42 -070096 if (bo == NULL)
97 return;
Eric Anholt6a9eb082008-06-03 09:27:37 -070098
Eric Anholtd70d6052009-10-06 12:40:42 -070099 bo->bufmgr->bo_unreference(bo);
Eric Anholt6a9eb082008-06-03 09:27:37 -0700100}
101
Emil Velikov0f8da822015-03-31 22:32:11 +0100102int
Maarten Lankhorst07fead42014-07-31 15:07:27 +0200103drm_intel_bo_map(drm_intel_bo *buf, int write_enable)
Eric Anholt6a9eb082008-06-03 09:27:37 -0700104{
Eric Anholtd70d6052009-10-06 12:40:42 -0700105 return buf->bufmgr->bo_map(buf, write_enable);
Eric Anholt6a9eb082008-06-03 09:27:37 -0700106}
107
Emil Velikov0f8da822015-03-31 22:32:11 +0100108int
Maarten Lankhorst07fead42014-07-31 15:07:27 +0200109drm_intel_bo_unmap(drm_intel_bo *buf)
Eric Anholt6a9eb082008-06-03 09:27:37 -0700110{
Eric Anholtd70d6052009-10-06 12:40:42 -0700111 return buf->bufmgr->bo_unmap(buf);
Eric Anholt6a9eb082008-06-03 09:27:37 -0700112}
113
Emil Velikov0f8da822015-03-31 22:32:11 +0100114int
Eric Anholt4b982642008-10-30 09:33:07 -0700115drm_intel_bo_subdata(drm_intel_bo *bo, unsigned long offset,
116 unsigned long size, const void *data)
Eric Anholt6a9eb082008-06-03 09:27:37 -0700117{
Eric Anholtf45305c2010-11-01 06:54:58 -0700118 return bo->bufmgr->bo_subdata(bo, offset, size, data);
Eric Anholt6a9eb082008-06-03 09:27:37 -0700119}
120
Emil Velikov0f8da822015-03-31 22:32:11 +0100121int
Eric Anholt4b982642008-10-30 09:33:07 -0700122drm_intel_bo_get_subdata(drm_intel_bo *bo, unsigned long offset,
123 unsigned long size, void *data)
Eric Anholt6a9eb082008-06-03 09:27:37 -0700124{
Eric Anholtd70d6052009-10-06 12:40:42 -0700125 int ret;
Yuanhan Liuce317a62011-07-20 16:08:51 +0800126 if (bo->bufmgr->bo_get_subdata)
Eric Anholtd70d6052009-10-06 12:40:42 -0700127 return bo->bufmgr->bo_get_subdata(bo, offset, size, data);
Eric Anholt6a9eb082008-06-03 09:27:37 -0700128
Eric Anholtd70d6052009-10-06 12:40:42 -0700129 if (size == 0 || data == NULL)
130 return 0;
Eric Anholt6a9eb082008-06-03 09:27:37 -0700131
Eric Anholtd70d6052009-10-06 12:40:42 -0700132 ret = drm_intel_bo_map(bo, 0);
133 if (ret)
134 return ret;
135 memcpy(data, (unsigned char *)bo->virtual + offset, size);
136 drm_intel_bo_unmap(bo);
137 return 0;
Eric Anholt6a9eb082008-06-03 09:27:37 -0700138}
139
Emil Velikov0f8da822015-03-31 22:32:11 +0100140void
Maarten Lankhorst07fead42014-07-31 15:07:27 +0200141drm_intel_bo_wait_rendering(drm_intel_bo *bo)
Eric Anholt6a9eb082008-06-03 09:27:37 -0700142{
Eric Anholtd70d6052009-10-06 12:40:42 -0700143 bo->bufmgr->bo_wait_rendering(bo);
Eric Anholt6a9eb082008-06-03 09:27:37 -0700144}
145
Emil Velikov0f8da822015-03-31 22:32:11 +0100146void
Maarten Lankhorst07fead42014-07-31 15:07:27 +0200147drm_intel_bufmgr_destroy(drm_intel_bufmgr *bufmgr)
Eric Anholt6a9eb082008-06-03 09:27:37 -0700148{
Eric Anholtd70d6052009-10-06 12:40:42 -0700149 bufmgr->destroy(bufmgr);
Eric Anholt6a9eb082008-06-03 09:27:37 -0700150}
151
Emil Velikov0f8da822015-03-31 22:32:11 +0100152int
Eric Anholt4b982642008-10-30 09:33:07 -0700153drm_intel_bo_exec(drm_intel_bo *bo, int used,
Eric Anholtd70d6052009-10-06 12:40:42 -0700154 drm_clip_rect_t * cliprects, int num_cliprects, int DR4)
Eric Anholt6a9eb082008-06-03 09:27:37 -0700155{
Eric Anholtd70d6052009-10-06 12:40:42 -0700156 return bo->bufmgr->bo_exec(bo, used, cliprects, num_cliprects, DR4);
Eric Anholt6a9eb082008-06-03 09:27:37 -0700157}
158
Emil Velikov0f8da822015-03-31 22:32:11 +0100159int
Zou Nan hai66375fd2010-06-02 10:07:37 +0800160drm_intel_bo_mrb_exec(drm_intel_bo *bo, int used,
161 drm_clip_rect_t *cliprects, int num_cliprects, int DR4,
Chris Wilson0184bb12010-12-19 13:01:15 +0000162 unsigned int rings)
Zou Nan hai66375fd2010-06-02 10:07:37 +0800163{
164 if (bo->bufmgr->bo_mrb_exec)
165 return bo->bufmgr->bo_mrb_exec(bo, used,
166 cliprects, num_cliprects, DR4,
Chris Wilson0184bb12010-12-19 13:01:15 +0000167 rings);
Zou Nan hai66375fd2010-06-02 10:07:37 +0800168
Chris Wilsonf395b0a2011-04-04 08:57:42 +0100169 switch (rings) {
170 case I915_EXEC_DEFAULT:
171 case I915_EXEC_RENDER:
Chris Wilson6717b752011-01-12 10:57:46 +0000172 return bo->bufmgr->bo_exec(bo, used,
173 cliprects, num_cliprects, DR4);
Chris Wilsonf395b0a2011-04-04 08:57:42 +0100174 default:
175 return -ENODEV;
176 }
Zou Nan hai66375fd2010-06-02 10:07:37 +0800177}
178
Emil Velikov0f8da822015-03-31 22:32:11 +0100179void
Maarten Lankhorst07fead42014-07-31 15:07:27 +0200180drm_intel_bufmgr_set_debug(drm_intel_bufmgr *bufmgr, int enable_debug)
Eric Anholt6a9eb082008-06-03 09:27:37 -0700181{
Eric Anholtd70d6052009-10-06 12:40:42 -0700182 bufmgr->debug = enable_debug;
Eric Anholt6a9eb082008-06-03 09:27:37 -0700183}
184
Emil Velikov0f8da822015-03-31 22:32:11 +0100185int
Maarten Lankhorst07fead42014-07-31 15:07:27 +0200186drm_intel_bufmgr_check_aperture_space(drm_intel_bo ** bo_array, int count)
Eric Anholt6a9eb082008-06-03 09:27:37 -0700187{
Eric Anholt46e92742008-08-08 13:13:46 -0700188 return bo_array[0]->bufmgr->check_aperture_space(bo_array, count);
Eric Anholt6a9eb082008-06-03 09:27:37 -0700189}
Eric Anholt738e36a2008-09-05 10:35:32 +0100190
Emil Velikov0f8da822015-03-31 22:32:11 +0100191int
Maarten Lankhorst07fead42014-07-31 15:07:27 +0200192drm_intel_bo_flink(drm_intel_bo *bo, uint32_t * name)
Eric Anholt738e36a2008-09-05 10:35:32 +0100193{
Eric Anholtd70d6052009-10-06 12:40:42 -0700194 if (bo->bufmgr->bo_flink)
195 return bo->bufmgr->bo_flink(bo, name);
Eric Anholt738e36a2008-09-05 10:35:32 +0100196
Eric Anholtd70d6052009-10-06 12:40:42 -0700197 return -ENODEV;
Eric Anholt738e36a2008-09-05 10:35:32 +0100198}
199
Emil Velikov0f8da822015-03-31 22:32:11 +0100200int
Eric Anholt4b982642008-10-30 09:33:07 -0700201drm_intel_bo_emit_reloc(drm_intel_bo *bo, uint32_t offset,
202 drm_intel_bo *target_bo, uint32_t target_offset,
203 uint32_t read_domains, uint32_t write_domain)
Eric Anholt738e36a2008-09-05 10:35:32 +0100204{
Eric Anholt4b982642008-10-30 09:33:07 -0700205 return bo->bufmgr->bo_emit_reloc(bo, offset,
206 target_bo, target_offset,
207 read_domains, write_domain);
Eric Anholt738e36a2008-09-05 10:35:32 +0100208}
209
Jesse Barnesb5096402009-09-15 11:02:58 -0700210/* For fence registers, not GL fences */
Emil Velikov0f8da822015-03-31 22:32:11 +0100211int
Jesse Barnesb5096402009-09-15 11:02:58 -0700212drm_intel_bo_emit_reloc_fence(drm_intel_bo *bo, uint32_t offset,
213 drm_intel_bo *target_bo, uint32_t target_offset,
214 uint32_t read_domains, uint32_t write_domain)
215{
216 return bo->bufmgr->bo_emit_reloc_fence(bo, offset,
217 target_bo, target_offset,
218 read_domains, write_domain);
219}
220
221
Emil Velikov0f8da822015-03-31 22:32:11 +0100222int
Maarten Lankhorst07fead42014-07-31 15:07:27 +0200223drm_intel_bo_pin(drm_intel_bo *bo, uint32_t alignment)
Eric Anholt738e36a2008-09-05 10:35:32 +0100224{
Eric Anholtd70d6052009-10-06 12:40:42 -0700225 if (bo->bufmgr->bo_pin)
226 return bo->bufmgr->bo_pin(bo, alignment);
Eric Anholt738e36a2008-09-05 10:35:32 +0100227
Eric Anholtd70d6052009-10-06 12:40:42 -0700228 return -ENODEV;
Eric Anholt738e36a2008-09-05 10:35:32 +0100229}
230
Emil Velikov0f8da822015-03-31 22:32:11 +0100231int
Maarten Lankhorst07fead42014-07-31 15:07:27 +0200232drm_intel_bo_unpin(drm_intel_bo *bo)
Eric Anholt738e36a2008-09-05 10:35:32 +0100233{
Eric Anholtd70d6052009-10-06 12:40:42 -0700234 if (bo->bufmgr->bo_unpin)
235 return bo->bufmgr->bo_unpin(bo);
Eric Anholt738e36a2008-09-05 10:35:32 +0100236
Eric Anholtd70d6052009-10-06 12:40:42 -0700237 return -ENODEV;
Eric Anholt738e36a2008-09-05 10:35:32 +0100238}
239
Emil Velikov0f8da822015-03-31 22:32:11 +0100240int
Maarten Lankhorst07fead42014-07-31 15:07:27 +0200241drm_intel_bo_set_tiling(drm_intel_bo *bo, uint32_t * tiling_mode,
242 uint32_t stride)
Eric Anholt738e36a2008-09-05 10:35:32 +0100243{
Eric Anholtd70d6052009-10-06 12:40:42 -0700244 if (bo->bufmgr->bo_set_tiling)
245 return bo->bufmgr->bo_set_tiling(bo, tiling_mode, stride);
Eric Anholt738e36a2008-09-05 10:35:32 +0100246
Eric Anholtd70d6052009-10-06 12:40:42 -0700247 *tiling_mode = I915_TILING_NONE;
248 return 0;
Eric Anholt738e36a2008-09-05 10:35:32 +0100249}
Eric Anholt99338382008-10-14 13:18:11 -0700250
Emil Velikov0f8da822015-03-31 22:32:11 +0100251int
Maarten Lankhorst07fead42014-07-31 15:07:27 +0200252drm_intel_bo_get_tiling(drm_intel_bo *bo, uint32_t * tiling_mode,
253 uint32_t * swizzle_mode)
Eric Anholt99338382008-10-14 13:18:11 -0700254{
Eric Anholtd70d6052009-10-06 12:40:42 -0700255 if (bo->bufmgr->bo_get_tiling)
256 return bo->bufmgr->bo_get_tiling(bo, tiling_mode, swizzle_mode);
Eric Anholt99338382008-10-14 13:18:11 -0700257
Eric Anholtd70d6052009-10-06 12:40:42 -0700258 *tiling_mode = I915_TILING_NONE;
259 *swizzle_mode = I915_BIT_6_SWIZZLE_NONE;
260 return 0;
Eric Anholt99338382008-10-14 13:18:11 -0700261}
Keith Packard5b5ce302009-05-11 13:42:12 -0700262
Emil Velikov0f8da822015-03-31 22:32:11 +0100263int
Michał Winiarski8b4d57e2015-09-09 16:07:10 +0200264drm_intel_bo_set_softpin_offset(drm_intel_bo *bo, uint64_t offset)
265{
266 if (bo->bufmgr->bo_set_softpin_offset)
267 return bo->bufmgr->bo_set_softpin_offset(bo, offset);
268
269 return -ENODEV;
270}
271
272int
Maarten Lankhorst07fead42014-07-31 15:07:27 +0200273drm_intel_bo_disable_reuse(drm_intel_bo *bo)
Keith Packard5b5ce302009-05-11 13:42:12 -0700274{
275 if (bo->bufmgr->bo_disable_reuse)
276 return bo->bufmgr->bo_disable_reuse(bo);
277 return 0;
278}
Keith Packardf57d7f42009-05-14 16:58:14 -0700279
Emil Velikov0f8da822015-03-31 22:32:11 +0100280int
Maarten Lankhorst07fead42014-07-31 15:07:27 +0200281drm_intel_bo_is_reusable(drm_intel_bo *bo)
Chris Wilson07e75892010-05-11 08:54:06 +0100282{
283 if (bo->bufmgr->bo_is_reusable)
284 return bo->bufmgr->bo_is_reusable(bo);
285 return 0;
286}
287
Emil Velikov0f8da822015-03-31 22:32:11 +0100288int
Maarten Lankhorst07fead42014-07-31 15:07:27 +0200289drm_intel_bo_busy(drm_intel_bo *bo)
Eric Anholt8214a652009-08-27 18:32:07 -0700290{
291 if (bo->bufmgr->bo_busy)
292 return bo->bufmgr->bo_busy(bo);
293 return 0;
294}
295
Emil Velikov0f8da822015-03-31 22:32:11 +0100296int
Maarten Lankhorst07fead42014-07-31 15:07:27 +0200297drm_intel_bo_madvise(drm_intel_bo *bo, int madv)
Chris Wilson83a35b62009-11-11 13:04:38 +0000298{
299 if (bo->bufmgr->bo_madvise)
300 return bo->bufmgr->bo_madvise(bo, madv);
301 return -1;
302}
303
Emil Velikov0f8da822015-03-31 22:32:11 +0100304int
Michel Thierry3350add2015-09-03 15:23:58 +0100305drm_intel_bo_use_48b_address_range(drm_intel_bo *bo, uint32_t enable)
306{
307 if (bo->bufmgr->bo_use_48b_address_range) {
308 bo->bufmgr->bo_use_48b_address_range(bo, enable);
309 return 0;
310 }
311
312 return -ENODEV;
313}
314
315int
Maarten Lankhorst07fead42014-07-31 15:07:27 +0200316drm_intel_bo_references(drm_intel_bo *bo, drm_intel_bo *target_bo)
Eric Anholt769b1052009-10-01 19:09:26 -0700317{
318 return bo->bufmgr->bo_references(bo, target_bo);
319}
320
Emil Velikov0f8da822015-03-31 22:32:11 +0100321int
Maarten Lankhorst07fead42014-07-31 15:07:27 +0200322drm_intel_get_pipe_from_crtc_id(drm_intel_bufmgr *bufmgr, int crtc_id)
Keith Packardf57d7f42009-05-14 16:58:14 -0700323{
324 if (bufmgr->get_pipe_from_crtc_id)
325 return bufmgr->get_pipe_from_crtc_id(bufmgr, crtc_id);
326 return -1;
327}
Chris Wilson9d776032011-06-04 12:47:19 +0100328
329static size_t
330drm_intel_probe_agp_aperture_size(int fd)
331{
332 struct pci_device *pci_dev;
333 size_t size = 0;
334 int ret;
335
336 ret = pci_system_init();
337 if (ret)
338 goto err;
339
340 /* XXX handle multiple adaptors? */
341 pci_dev = pci_device_find_by_slot(0, 0, 2, 0);
342 if (pci_dev == NULL)
343 goto err;
344
345 ret = pci_device_probe(pci_dev);
346 if (ret)
347 goto err;
348
349 size = pci_dev->regions[2].size;
350err:
351 pci_system_cleanup ();
352 return size;
353}
354
Emil Velikov0f8da822015-03-31 22:32:11 +0100355int
Maarten Lankhorst07fead42014-07-31 15:07:27 +0200356drm_intel_get_aperture_sizes(int fd, size_t *mappable, size_t *total)
Chris Wilson9d776032011-06-04 12:47:19 +0100357{
358
359 struct drm_i915_gem_get_aperture aperture;
360 int ret;
361
362 ret = drmIoctl(fd, DRM_IOCTL_I915_GEM_GET_APERTURE, &aperture);
363 if (ret)
364 return ret;
365
366 *mappable = 0;
367 /* XXX add a query for the kernel value? */
368 if (*mappable == 0)
369 *mappable = drm_intel_probe_agp_aperture_size(fd);
370 if (*mappable == 0)
371 *mappable = 64 * 1024 * 1024; /* minimum possible value */
372 *total = aperture.aper_size;
373 return 0;
374}