struct fd_bo * fd_bo_from_name(struct fd_device *dev, uint32_t name) { struct drm_gem_open req = { .name = name, }; struct fd_bo *bo; pthread_mutex_lock(&table_lock); /* check name table first, to see if bo is already open: */ bo = lookup_bo(dev->name_table, name); if (bo) goto out_unlock; if (drmIoctl(dev->fd, DRM_IOCTL_GEM_OPEN, &req)) { ERROR_MSG("gem-open failed: %s", strerror(errno)); goto out_unlock; } bo = lookup_bo(dev->handle_table, req.handle); if (bo) goto out_unlock; bo = bo_from_handle(dev, req.size, req.handle); if (bo) { set_name(bo, name); VG_BO_ALLOC(bo); } out_unlock: pthread_mutex_unlock(&table_lock); return bo; }
struct fd_bo * fd_bo_from_dmabuf(struct fd_device *dev, int fd) { int ret, size; uint32_t handle; struct fd_bo *bo; pthread_mutex_lock(&table_lock); ret = drmPrimeFDToHandle(dev->fd, fd, &handle); if (ret) { return NULL; } bo = lookup_bo(dev->handle_table, handle); if (bo) goto out_unlock; /* lseek() to get bo size */ size = lseek(fd, 0, SEEK_END); lseek(fd, 0, SEEK_CUR); bo = bo_from_handle(dev, size, handle); VG_BO_ALLOC(bo); out_unlock: pthread_mutex_unlock(&table_lock); return bo; }
drm_public int drm_tegra_bo_wrap(struct drm_tegra_bo **bop, struct drm_tegra *drm, uint32_t handle, uint32_t flags, uint32_t size) { struct drm_tegra_bo *bo; int err = 0; if (!drm || !bop) return -EINVAL; pthread_mutex_lock(&table_lock); /* check handle table to see if BO is already open */ bo = lookup_bo(drm->handle_table, handle); if (bo) goto unlock; bo = calloc(1, sizeof(*bo)); if (!bo) { err = -ENOMEM; goto unlock; } DRMINITLISTHEAD(&bo->push_list); DRMINITLISTHEAD(&bo->bo_list); atomic_set(&bo->ref, 1); bo->handle = handle; bo->flags = flags; bo->size = size; bo->drm = drm; VG_BO_ALLOC(bo); /* add ourselves into the handle table */ drmHashInsert(drm->handle_table, handle, bo); DBG_BO(bo, "success\n"); unlock: pthread_mutex_unlock(&table_lock); *bop = bo; return err; }
struct fd_bo * fd_bo_from_handle(struct fd_device *dev, uint32_t handle, uint32_t size) { struct fd_bo *bo = NULL; pthread_mutex_lock(&table_lock); bo = lookup_bo(dev->handle_table, handle); if (bo) goto out_unlock; bo = bo_from_handle(dev, size, handle); VG_BO_ALLOC(bo); out_unlock: pthread_mutex_unlock(&table_lock); return bo; }
struct fd_bo * fd_bo_new(struct fd_device *dev, uint32_t size, uint32_t flags) { struct fd_bo *bo = NULL; uint32_t handle; int ret; bo = fd_bo_cache_alloc(&dev->bo_cache, &size, flags); if (bo) return bo; ret = dev->funcs->bo_new_handle(dev, size, flags, &handle); if (ret) return NULL; pthread_mutex_lock(&table_lock); bo = bo_from_handle(dev, size, handle); bo->bo_reuse = TRUE; pthread_mutex_unlock(&table_lock); VG_BO_ALLOC(bo); return bo; }
drm_public int drm_tegra_bo_from_dmabuf(struct drm_tegra_bo **bop, struct drm_tegra *drm, int fd, uint32_t flags) { struct drm_tegra_bo *dup; struct drm_tegra_bo *bo; uint32_t handle; uint32_t size; int err; if (!drm || !bop) return -EINVAL; pthread_mutex_lock(&table_lock); bo = calloc(1, sizeof(*bo)); if (!bo) { err = -ENOMEM; goto unlock; } err = drmPrimeFDToHandle(drm->fd, fd, &handle); if (err) { free(bo); bo = NULL; goto unlock; } /* check handle table to see if BO is already open */ dup = lookup_bo(drm->handle_table, handle); if (dup) { DBG_BO(dup, "success reused\n"); free(bo); bo = dup; goto unlock; } errno = 0; /* lseek() to get bo size */ size = lseek(fd, 0, SEEK_END); lseek(fd, 0, SEEK_CUR); /* store lseek() error number */ err = -errno; atomic_set(&bo->ref, 1); bo->handle = handle; bo->flags = flags; bo->size = size; bo->drm = drm; VG_BO_ALLOC(bo); /* add ourself into the handle table: */ drmHashInsert(drm->handle_table, handle, bo); /* handle lseek() error */ if (err) { VDBG_BO(bo, "lseek failed %d (%s)\n", err, strerror(-err)); drm_tegra_bo_free(bo); bo = NULL; } else { DBG_BO(bo, "success\n"); } unlock: pthread_mutex_unlock(&table_lock); *bop = bo; return err; }
drm_public int drm_tegra_bo_from_name(struct drm_tegra_bo **bop, struct drm_tegra *drm, uint32_t name, uint32_t flags) { struct drm_gem_open args; struct drm_tegra_bo *dup; struct drm_tegra_bo *bo; int err = 0; if (!drm || !name || !bop) return -EINVAL; pthread_mutex_lock(&table_lock); /* check name table first, to see if BO is already open */ bo = lookup_bo(drm->name_table, name); if (bo) goto unlock; bo = calloc(1, sizeof(*bo)); if (!bo) { err = -ENOMEM; goto unlock; } memset(&args, 0, sizeof(args)); args.name = name; err = drmIoctl(drm->fd, DRM_IOCTL_GEM_OPEN, &args); if (err < 0) { VDBG_DRM(drm, "failed name 0x%08X err %d strerror(%s)\n", name, err, strerror(-err)); err = -errno; free(bo); bo = NULL; goto unlock; } /* check handle table second, to see if BO is already open */ dup = lookup_bo(drm->handle_table, args.handle); if (dup) { VDBG_BO(dup, "success reused name 0x%08X\n", name); free(bo); bo = dup; goto unlock; } drmHashInsert(drm->name_table, name, bo); atomic_set(&bo->ref, 1); bo->name = name; bo->handle = args.handle; bo->flags = flags; bo->size = args.size; bo->drm = drm; DBG_BO(bo, "success\n"); VG_BO_ALLOC(bo); unlock: pthread_mutex_unlock(&table_lock); *bop = bo; return err; }
drm_public int drm_tegra_bo_new(struct drm_tegra_bo **bop, struct drm_tegra *drm, uint32_t flags, uint32_t size) { struct drm_tegra_gem_create args; struct drm_tegra_bo *bo; int err; if (!drm || size == 0 || !bop) return -EINVAL; pthread_mutex_lock(&table_lock); bo = drm_tegra_bo_cache_alloc(drm, &size, flags); pthread_mutex_unlock(&table_lock); if (bo) { DBG_BO(bo, "success from cache\n"); goto out; } bo = calloc(1, sizeof(*bo)); if (!bo) return -ENOMEM; DRMINITLISTHEAD(&bo->push_list); DRMINITLISTHEAD(&bo->bo_list); atomic_set(&bo->ref, 1); bo->reuse = true; bo->flags = flags; bo->size = size; bo->drm = drm; memset(&args, 0, sizeof(args)); args.flags = flags; args.size = size; #ifndef NDEBUG if (drm->debug_bo_front_guard) { bo->offset += 4096; args.size += 4096; } if (drm->debug_bo_back_guard) args.size += 4096; #endif err = drmCommandWriteRead(drm->fd, DRM_TEGRA_GEM_CREATE, &args, sizeof(args)); if (err < 0) { VDBG_DRM(drm, "failed size %u bytes flags 0x%08X err %d (%s)\n", size, flags, err, strerror(-err)); free(bo); return err; } bo->handle = args.handle; DBG_BO(bo, "success new\n"); VG_BO_ALLOC(bo); #ifndef NDEBUG if (drm->debug_bo) { bo->debug_size = align(bo->size, 4096); drm->debug_bos_total_size += bo->debug_size; drm->debug_bos_allocated++; } #endif drm_tegra_bo_setup_guards(bo); DBG_BO_STATS(drm); pthread_mutex_lock(&table_lock); /* add ourselves into the handle table */ drmHashInsert(drm->handle_table, args.handle, bo); pthread_mutex_unlock(&table_lock); out: *bop = bo; return 0; }