+
+ bo->tiling = gem_get_tiling.tiling_mode;
+ return 0;
+}
+
+static void *i915_bo_map(struct bo *bo, struct map_info *data, size_t plane, int prot)
+{
+ int ret;
+ void *addr;
+ struct drm_i915_gem_set_domain set_domain;
+
+ memset(&set_domain, 0, sizeof(set_domain));
+ set_domain.handle = bo->handles[0].u32;
+ if (bo->tiling == I915_TILING_NONE) {
+ struct drm_i915_gem_mmap gem_map;
+ memset(&gem_map, 0, sizeof(gem_map));
+
+ gem_map.handle = bo->handles[0].u32;
+ gem_map.offset = 0;
+ gem_map.size = bo->total_size;
+
+ ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_MMAP, &gem_map);
+ if (ret) {
+ fprintf(stderr, "drv: DRM_IOCTL_I915_GEM_MMAP failed\n");
+ return MAP_FAILED;
+ }
+
+ addr = (void *)(uintptr_t)gem_map.addr_ptr;
+ set_domain.read_domains = I915_GEM_DOMAIN_CPU;
+ set_domain.write_domain = I915_GEM_DOMAIN_CPU;
+
+ } else {
+ struct drm_i915_gem_mmap_gtt gem_map;
+ memset(&gem_map, 0, sizeof(gem_map));
+
+ gem_map.handle = bo->handles[0].u32;
+
+ ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_MMAP_GTT, &gem_map);
+ if (ret) {
+ fprintf(stderr, "drv: DRM_IOCTL_I915_GEM_MMAP_GTT failed\n");
+ return MAP_FAILED;
+ }
+
+ addr = mmap(0, bo->total_size, prot, MAP_SHARED, bo->drv->fd, gem_map.offset);
+ set_domain.read_domains = I915_GEM_DOMAIN_GTT;
+ set_domain.write_domain = I915_GEM_DOMAIN_GTT;
+ }
+
+ if (addr == MAP_FAILED) {
+ fprintf(stderr, "drv: i915 GEM mmap failed\n");
+ return addr;
+ }
+
+ ret = drmIoctl(bo->drv->fd, DRM_IOCTL_I915_GEM_SET_DOMAIN, &set_domain);
+ if (ret) {
+ fprintf(stderr, "drv: DRM_IOCTL_I915_GEM_SET_DOMAIN failed\n");
+ return MAP_FAILED;
+ }
+
+ data->length = bo->total_size;
+ return addr;
+}
+
+static int i915_bo_unmap(struct bo *bo, struct map_info *data)
+{
+ struct i915_device *i915 = bo->drv->priv;
+ if (!i915->has_llc && bo->tiling == I915_TILING_NONE)
+ i915_clflush(data->addr, data->length);
+
+ return munmap(data->addr, data->length);
+}
+
+static uint32_t i915_resolve_format(uint32_t format, uint64_t usage)
+{
+ switch (format) {
+ case DRM_FORMAT_FLEX_IMPLEMENTATION_DEFINED:
+ /* KBL camera subsystem requires NV12. */
+ if (usage & (BO_USE_HW_CAMERA_READ | BO_USE_HW_CAMERA_WRITE))
+ return DRM_FORMAT_NV12;
+ /*HACK: See b/28671744 */
+ return DRM_FORMAT_XBGR8888;
+ case DRM_FORMAT_FLEX_YCbCr_420_888:
+ /* KBL camera subsystem requires NV12. */
+ if (usage & (BO_USE_HW_CAMERA_READ | BO_USE_HW_CAMERA_WRITE))
+ return DRM_FORMAT_NV12;
+ return DRM_FORMAT_YVU420;
+ default:
+ return format;
+ }
+}
+
+struct backend backend_i915 = {
+ .name = "i915",
+ .init = i915_init,
+ .close = i915_close,
+ .bo_create = i915_bo_create,
+ .bo_destroy = drv_gem_bo_destroy,
+ .bo_import = i915_bo_import,
+ .bo_map = i915_bo_map,
+ .bo_unmap = i915_bo_unmap,
+ .resolve_format = i915_resolve_format,