gem_glue.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. #include "gem_glue.h"
  22. void drm_gem_object_release_wrap(struct drm_gem_object *obj)
  23. {
  24. /* Remove the list map if one is present */
  25. if (obj->map_list.map) {
  26. struct drm_gem_mm *mm = obj->dev->mm_private;
  27. struct drm_map_list *list = &obj->map_list;
  28. drm_ht_remove_item(&mm->offset_hash, &list->hash);
  29. drm_mm_put_block(list->file_offset_node);
  30. kfree(list->map);
  31. list->map = NULL;
  32. }
  33. drm_gem_object_release(obj);
  34. }
  35. /**
  36. * gem_create_mmap_offset - invent an mmap offset
  37. * @obj: our object
  38. *
  39. * Standard implementation of offset generation for mmap as is
  40. * duplicated in several drivers. This belongs in GEM.
  41. */
  42. int gem_create_mmap_offset(struct drm_gem_object *obj)
  43. {
  44. struct drm_device *dev = obj->dev;
  45. struct drm_gem_mm *mm = dev->mm_private;
  46. struct drm_map_list *list;
  47. struct drm_local_map *map;
  48. int ret;
  49. list = &obj->map_list;
  50. list->map = kzalloc(sizeof(struct drm_map_list), GFP_KERNEL);
  51. if (list->map == NULL)
  52. return -ENOMEM;
  53. map = list->map;
  54. map->type = _DRM_GEM;
  55. map->size = obj->size;
  56. map->handle = obj;
  57. list->file_offset_node = drm_mm_search_free(&mm->offset_manager,
  58. obj->size / PAGE_SIZE, 0, 0);
  59. if (!list->file_offset_node) {
  60. dev_err(dev->dev, "failed to allocate offset for bo %d\n",
  61. obj->name);
  62. ret = -ENOSPC;
  63. goto free_it;
  64. }
  65. list->file_offset_node = drm_mm_get_block(list->file_offset_node,
  66. obj->size / PAGE_SIZE, 0);
  67. if (!list->file_offset_node) {
  68. ret = -ENOMEM;
  69. goto free_it;
  70. }
  71. list->hash.key = list->file_offset_node->start;
  72. ret = drm_ht_insert_item(&mm->offset_hash, &list->hash);
  73. if (ret) {
  74. dev_err(dev->dev, "failed to add to map hash\n");
  75. goto free_mm;
  76. }
  77. return 0;
  78. free_mm:
  79. drm_mm_put_block(list->file_offset_node);
  80. free_it:
  81. kfree(list->map);
  82. list->map = NULL;
  83. return ret;
  84. }