gem_glue.c 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /**************************************************************************
  2. * Copyright (c) 2011, Intel Corporation.
  3. * All Rights Reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * this program; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  17. *
  18. **************************************************************************/
  19. #include <drm/drmP.h>
  20. #include <drm/drm.h>
  21. void drm_gem_object_release_wrap(struct drm_gem_object *obj)
  22. {
  23. /* Remove the list map if one is present */
  24. if (obj->map_list.map) {
  25. struct drm_gem_mm *mm = obj->dev->mm_private;
  26. struct drm_map_list *list = &obj->map_list;
  27. drm_ht_remove_item(&mm->offset_hash, &list->hash);
  28. drm_mm_put_block(list->file_offset_node);
  29. kfree(list->map);
  30. list->map = NULL;
  31. }
  32. drm_gem_object_release(obj);
  33. }
  34. /**
  35. * gem_create_mmap_offset - invent an mmap offset
  36. * @obj: our object
  37. *
  38. * Standard implementation of offset generation for mmap as is
  39. * duplicated in several drivers. This belongs in GEM.
  40. */
  41. int gem_create_mmap_offset(struct drm_gem_object *obj)
  42. {
  43. struct drm_device *dev = obj->dev;
  44. struct drm_gem_mm *mm = dev->mm_private;
  45. struct drm_map_list *list;
  46. struct drm_local_map *map;
  47. int ret;
  48. list = &obj->map_list;
  49. list->map = kzalloc(sizeof(struct drm_map_list), GFP_KERNEL);
  50. if (list->map == NULL)
  51. return -ENOMEM;
  52. map = list->map;
  53. map->type = _DRM_GEM;
  54. map->size = obj->size;
  55. map->handle = obj;
  56. list->file_offset_node = drm_mm_search_free(&mm->offset_manager,
  57. obj->size / PAGE_SIZE, 0, 0);
  58. if (!list->file_offset_node) {
  59. dev_err(dev->dev, "failed to allocate offset for bo %d\n",
  60. obj->name);
  61. ret = -ENOSPC;
  62. goto free_it;
  63. }
  64. list->file_offset_node = drm_mm_get_block(list->file_offset_node,
  65. obj->size / PAGE_SIZE, 0);
  66. if (!list->file_offset_node) {
  67. ret = -ENOMEM;
  68. goto free_it;
  69. }
  70. list->hash.key = list->file_offset_node->start;
  71. ret = drm_ht_insert_item(&mm->offset_hash, &list->hash);
  72. if (ret) {
  73. dev_err(dev->dev, "failed to add to map hash\n");
  74. goto free_mm;
  75. }
  76. return 0;
  77. free_mm:
  78. drm_mm_put_block(list->file_offset_node);
  79. free_it:
  80. kfree(list->map);
  81. list->map = NULL;
  82. return ret;
  83. }