bochs_mm.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License as published by
  4. * the Free Software Foundation; either version 2 of the License, or
  5. * (at your option) any later version.
  6. */
  7. #include "bochs.h"
  8. static void bochs_ttm_placement(struct bochs_bo *bo, int domain);
  9. /* ---------------------------------------------------------------------- */
  10. static inline struct bochs_device *bochs_bdev(struct ttm_bo_device *bd)
  11. {
  12. return container_of(bd, struct bochs_device, ttm.bdev);
  13. }
  14. static int bochs_ttm_mem_global_init(struct drm_global_reference *ref)
  15. {
  16. return ttm_mem_global_init(ref->object);
  17. }
  18. static void bochs_ttm_mem_global_release(struct drm_global_reference *ref)
  19. {
  20. ttm_mem_global_release(ref->object);
  21. }
  22. static int bochs_ttm_global_init(struct bochs_device *bochs)
  23. {
  24. struct drm_global_reference *global_ref;
  25. int r;
  26. global_ref = &bochs->ttm.mem_global_ref;
  27. global_ref->global_type = DRM_GLOBAL_TTM_MEM;
  28. global_ref->size = sizeof(struct ttm_mem_global);
  29. global_ref->init = &bochs_ttm_mem_global_init;
  30. global_ref->release = &bochs_ttm_mem_global_release;
  31. r = drm_global_item_ref(global_ref);
  32. if (r != 0) {
  33. DRM_ERROR("Failed setting up TTM memory accounting "
  34. "subsystem.\n");
  35. return r;
  36. }
  37. bochs->ttm.bo_global_ref.mem_glob =
  38. bochs->ttm.mem_global_ref.object;
  39. global_ref = &bochs->ttm.bo_global_ref.ref;
  40. global_ref->global_type = DRM_GLOBAL_TTM_BO;
  41. global_ref->size = sizeof(struct ttm_bo_global);
  42. global_ref->init = &ttm_bo_global_init;
  43. global_ref->release = &ttm_bo_global_release;
  44. r = drm_global_item_ref(global_ref);
  45. if (r != 0) {
  46. DRM_ERROR("Failed setting up TTM BO subsystem.\n");
  47. drm_global_item_unref(&bochs->ttm.mem_global_ref);
  48. return r;
  49. }
  50. return 0;
  51. }
  52. static void bochs_ttm_global_release(struct bochs_device *bochs)
  53. {
  54. if (bochs->ttm.mem_global_ref.release == NULL)
  55. return;
  56. drm_global_item_unref(&bochs->ttm.bo_global_ref.ref);
  57. drm_global_item_unref(&bochs->ttm.mem_global_ref);
  58. bochs->ttm.mem_global_ref.release = NULL;
  59. }
  60. static void bochs_bo_ttm_destroy(struct ttm_buffer_object *tbo)
  61. {
  62. struct bochs_bo *bo;
  63. bo = container_of(tbo, struct bochs_bo, bo);
  64. drm_gem_object_release(&bo->gem);
  65. kfree(bo);
  66. }
  67. static bool bochs_ttm_bo_is_bochs_bo(struct ttm_buffer_object *bo)
  68. {
  69. if (bo->destroy == &bochs_bo_ttm_destroy)
  70. return true;
  71. return false;
  72. }
  73. static int bochs_bo_init_mem_type(struct ttm_bo_device *bdev, uint32_t type,
  74. struct ttm_mem_type_manager *man)
  75. {
  76. switch (type) {
  77. case TTM_PL_SYSTEM:
  78. man->flags = TTM_MEMTYPE_FLAG_MAPPABLE;
  79. man->available_caching = TTM_PL_MASK_CACHING;
  80. man->default_caching = TTM_PL_FLAG_CACHED;
  81. break;
  82. case TTM_PL_VRAM:
  83. man->func = &ttm_bo_manager_func;
  84. man->flags = TTM_MEMTYPE_FLAG_FIXED |
  85. TTM_MEMTYPE_FLAG_MAPPABLE;
  86. man->available_caching = TTM_PL_FLAG_UNCACHED |
  87. TTM_PL_FLAG_WC;
  88. man->default_caching = TTM_PL_FLAG_WC;
  89. break;
  90. default:
  91. DRM_ERROR("Unsupported memory type %u\n", (unsigned)type);
  92. return -EINVAL;
  93. }
  94. return 0;
  95. }
  96. static void
  97. bochs_bo_evict_flags(struct ttm_buffer_object *bo, struct ttm_placement *pl)
  98. {
  99. struct bochs_bo *bochsbo = bochs_bo(bo);
  100. if (!bochs_ttm_bo_is_bochs_bo(bo))
  101. return;
  102. bochs_ttm_placement(bochsbo, TTM_PL_FLAG_SYSTEM);
  103. *pl = bochsbo->placement;
  104. }
  105. static int bochs_bo_verify_access(struct ttm_buffer_object *bo,
  106. struct file *filp)
  107. {
  108. struct bochs_bo *bochsbo = bochs_bo(bo);
  109. return drm_vma_node_verify_access(&bochsbo->gem.vma_node, filp);
  110. }
  111. static int bochs_ttm_io_mem_reserve(struct ttm_bo_device *bdev,
  112. struct ttm_mem_reg *mem)
  113. {
  114. struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
  115. struct bochs_device *bochs = bochs_bdev(bdev);
  116. mem->bus.addr = NULL;
  117. mem->bus.offset = 0;
  118. mem->bus.size = mem->num_pages << PAGE_SHIFT;
  119. mem->bus.base = 0;
  120. mem->bus.is_iomem = false;
  121. if (!(man->flags & TTM_MEMTYPE_FLAG_MAPPABLE))
  122. return -EINVAL;
  123. switch (mem->mem_type) {
  124. case TTM_PL_SYSTEM:
  125. /* system memory */
  126. return 0;
  127. case TTM_PL_VRAM:
  128. mem->bus.offset = mem->start << PAGE_SHIFT;
  129. mem->bus.base = bochs->fb_base;
  130. mem->bus.is_iomem = true;
  131. break;
  132. default:
  133. return -EINVAL;
  134. break;
  135. }
  136. return 0;
  137. }
  138. static void bochs_ttm_io_mem_free(struct ttm_bo_device *bdev,
  139. struct ttm_mem_reg *mem)
  140. {
  141. }
  142. static int bochs_bo_move(struct ttm_buffer_object *bo,
  143. bool evict, bool interruptible,
  144. bool no_wait_gpu,
  145. struct ttm_mem_reg *new_mem)
  146. {
  147. return ttm_bo_move_memcpy(bo, evict, no_wait_gpu, new_mem);
  148. }
  149. static void bochs_ttm_backend_destroy(struct ttm_tt *tt)
  150. {
  151. ttm_tt_fini(tt);
  152. kfree(tt);
  153. }
  154. static struct ttm_backend_func bochs_tt_backend_func = {
  155. .destroy = &bochs_ttm_backend_destroy,
  156. };
  157. static struct ttm_tt *bochs_ttm_tt_create(struct ttm_bo_device *bdev,
  158. unsigned long size,
  159. uint32_t page_flags,
  160. struct page *dummy_read_page)
  161. {
  162. struct ttm_tt *tt;
  163. tt = kzalloc(sizeof(struct ttm_tt), GFP_KERNEL);
  164. if (tt == NULL)
  165. return NULL;
  166. tt->func = &bochs_tt_backend_func;
  167. if (ttm_tt_init(tt, bdev, size, page_flags, dummy_read_page)) {
  168. kfree(tt);
  169. return NULL;
  170. }
  171. return tt;
  172. }
  173. struct ttm_bo_driver bochs_bo_driver = {
  174. .ttm_tt_create = bochs_ttm_tt_create,
  175. .ttm_tt_populate = ttm_pool_populate,
  176. .ttm_tt_unpopulate = ttm_pool_unpopulate,
  177. .init_mem_type = bochs_bo_init_mem_type,
  178. .evict_flags = bochs_bo_evict_flags,
  179. .move = bochs_bo_move,
  180. .verify_access = bochs_bo_verify_access,
  181. .io_mem_reserve = &bochs_ttm_io_mem_reserve,
  182. .io_mem_free = &bochs_ttm_io_mem_free,
  183. };
  184. int bochs_mm_init(struct bochs_device *bochs)
  185. {
  186. struct ttm_bo_device *bdev = &bochs->ttm.bdev;
  187. int ret;
  188. ret = bochs_ttm_global_init(bochs);
  189. if (ret)
  190. return ret;
  191. ret = ttm_bo_device_init(&bochs->ttm.bdev,
  192. bochs->ttm.bo_global_ref.ref.object,
  193. &bochs_bo_driver, DRM_FILE_PAGE_OFFSET,
  194. true);
  195. if (ret) {
  196. DRM_ERROR("Error initialising bo driver; %d\n", ret);
  197. return ret;
  198. }
  199. ret = ttm_bo_init_mm(bdev, TTM_PL_VRAM,
  200. bochs->fb_size >> PAGE_SHIFT);
  201. if (ret) {
  202. DRM_ERROR("Failed ttm VRAM init: %d\n", ret);
  203. return ret;
  204. }
  205. bochs->ttm.initialized = true;
  206. return 0;
  207. }
  208. void bochs_mm_fini(struct bochs_device *bochs)
  209. {
  210. if (!bochs->ttm.initialized)
  211. return;
  212. ttm_bo_device_release(&bochs->ttm.bdev);
  213. bochs_ttm_global_release(bochs);
  214. bochs->ttm.initialized = false;
  215. }
  216. static void bochs_ttm_placement(struct bochs_bo *bo, int domain)
  217. {
  218. u32 c = 0;
  219. bo->placement.fpfn = 0;
  220. bo->placement.lpfn = 0;
  221. bo->placement.placement = bo->placements;
  222. bo->placement.busy_placement = bo->placements;
  223. if (domain & TTM_PL_FLAG_VRAM) {
  224. bo->placements[c++] = TTM_PL_FLAG_WC | TTM_PL_FLAG_UNCACHED
  225. | TTM_PL_FLAG_VRAM;
  226. }
  227. if (domain & TTM_PL_FLAG_SYSTEM) {
  228. bo->placements[c++] = TTM_PL_MASK_CACHING | TTM_PL_FLAG_SYSTEM;
  229. }
  230. if (!c) {
  231. bo->placements[c++] = TTM_PL_MASK_CACHING | TTM_PL_FLAG_SYSTEM;
  232. }
  233. bo->placement.num_placement = c;
  234. bo->placement.num_busy_placement = c;
  235. }
  236. static inline u64 bochs_bo_gpu_offset(struct bochs_bo *bo)
  237. {
  238. return bo->bo.offset;
  239. }
  240. int bochs_bo_pin(struct bochs_bo *bo, u32 pl_flag, u64 *gpu_addr)
  241. {
  242. int i, ret;
  243. if (bo->pin_count) {
  244. bo->pin_count++;
  245. if (gpu_addr)
  246. *gpu_addr = bochs_bo_gpu_offset(bo);
  247. return 0;
  248. }
  249. bochs_ttm_placement(bo, pl_flag);
  250. for (i = 0; i < bo->placement.num_placement; i++)
  251. bo->placements[i] |= TTM_PL_FLAG_NO_EVICT;
  252. ret = ttm_bo_validate(&bo->bo, &bo->placement, false, false);
  253. if (ret)
  254. return ret;
  255. bo->pin_count = 1;
  256. if (gpu_addr)
  257. *gpu_addr = bochs_bo_gpu_offset(bo);
  258. return 0;
  259. }
  260. int bochs_bo_unpin(struct bochs_bo *bo)
  261. {
  262. int i, ret;
  263. if (!bo->pin_count) {
  264. DRM_ERROR("unpin bad %p\n", bo);
  265. return 0;
  266. }
  267. bo->pin_count--;
  268. if (bo->pin_count)
  269. return 0;
  270. for (i = 0; i < bo->placement.num_placement; i++)
  271. bo->placements[i] &= ~TTM_PL_FLAG_NO_EVICT;
  272. ret = ttm_bo_validate(&bo->bo, &bo->placement, false, false);
  273. if (ret)
  274. return ret;
  275. return 0;
  276. }
  277. int bochs_mmap(struct file *filp, struct vm_area_struct *vma)
  278. {
  279. struct drm_file *file_priv;
  280. struct bochs_device *bochs;
  281. if (unlikely(vma->vm_pgoff < DRM_FILE_PAGE_OFFSET))
  282. return drm_mmap(filp, vma);
  283. file_priv = filp->private_data;
  284. bochs = file_priv->minor->dev->dev_private;
  285. return ttm_bo_mmap(filp, vma, &bochs->ttm.bdev);
  286. }
  287. /* ---------------------------------------------------------------------- */
  288. static int bochs_bo_create(struct drm_device *dev, int size, int align,
  289. uint32_t flags, struct bochs_bo **pbochsbo)
  290. {
  291. struct bochs_device *bochs = dev->dev_private;
  292. struct bochs_bo *bochsbo;
  293. size_t acc_size;
  294. int ret;
  295. bochsbo = kzalloc(sizeof(struct bochs_bo), GFP_KERNEL);
  296. if (!bochsbo)
  297. return -ENOMEM;
  298. ret = drm_gem_object_init(dev, &bochsbo->gem, size);
  299. if (ret) {
  300. kfree(bochsbo);
  301. return ret;
  302. }
  303. bochsbo->bo.bdev = &bochs->ttm.bdev;
  304. bochsbo->bo.bdev->dev_mapping = dev->dev_mapping;
  305. bochs_ttm_placement(bochsbo, TTM_PL_FLAG_VRAM | TTM_PL_FLAG_SYSTEM);
  306. acc_size = ttm_bo_dma_acc_size(&bochs->ttm.bdev, size,
  307. sizeof(struct bochs_bo));
  308. ret = ttm_bo_init(&bochs->ttm.bdev, &bochsbo->bo, size,
  309. ttm_bo_type_device, &bochsbo->placement,
  310. align >> PAGE_SHIFT, false, NULL, acc_size,
  311. NULL, bochs_bo_ttm_destroy);
  312. if (ret)
  313. return ret;
  314. *pbochsbo = bochsbo;
  315. return 0;
  316. }
  317. int bochs_gem_create(struct drm_device *dev, u32 size, bool iskernel,
  318. struct drm_gem_object **obj)
  319. {
  320. struct bochs_bo *bochsbo;
  321. int ret;
  322. *obj = NULL;
  323. size = ALIGN(size, PAGE_SIZE);
  324. if (size == 0)
  325. return -EINVAL;
  326. ret = bochs_bo_create(dev, size, 0, 0, &bochsbo);
  327. if (ret) {
  328. if (ret != -ERESTARTSYS)
  329. DRM_ERROR("failed to allocate GEM object\n");
  330. return ret;
  331. }
  332. *obj = &bochsbo->gem;
  333. return 0;
  334. }
  335. int bochs_dumb_create(struct drm_file *file, struct drm_device *dev,
  336. struct drm_mode_create_dumb *args)
  337. {
  338. struct drm_gem_object *gobj;
  339. u32 handle;
  340. int ret;
  341. args->pitch = args->width * ((args->bpp + 7) / 8);
  342. args->size = args->pitch * args->height;
  343. ret = bochs_gem_create(dev, args->size, false,
  344. &gobj);
  345. if (ret)
  346. return ret;
  347. ret = drm_gem_handle_create(file, gobj, &handle);
  348. drm_gem_object_unreference_unlocked(gobj);
  349. if (ret)
  350. return ret;
  351. args->handle = handle;
  352. return 0;
  353. }
  354. static void bochs_bo_unref(struct bochs_bo **bo)
  355. {
  356. struct ttm_buffer_object *tbo;
  357. if ((*bo) == NULL)
  358. return;
  359. tbo = &((*bo)->bo);
  360. ttm_bo_unref(&tbo);
  361. if (tbo == NULL)
  362. *bo = NULL;
  363. }
  364. void bochs_gem_free_object(struct drm_gem_object *obj)
  365. {
  366. struct bochs_bo *bochs_bo = gem_to_bochs_bo(obj);
  367. if (!bochs_bo)
  368. return;
  369. bochs_bo_unref(&bochs_bo);
  370. }
  371. int bochs_dumb_mmap_offset(struct drm_file *file, struct drm_device *dev,
  372. uint32_t handle, uint64_t *offset)
  373. {
  374. struct drm_gem_object *obj;
  375. int ret;
  376. struct bochs_bo *bo;
  377. mutex_lock(&dev->struct_mutex);
  378. obj = drm_gem_object_lookup(dev, file, handle);
  379. if (obj == NULL) {
  380. ret = -ENOENT;
  381. goto out_unlock;
  382. }
  383. bo = gem_to_bochs_bo(obj);
  384. *offset = bochs_bo_mmap_offset(bo);
  385. drm_gem_object_unreference(obj);
  386. ret = 0;
  387. out_unlock:
  388. mutex_unlock(&dev->struct_mutex);
  389. return ret;
  390. }
  391. /* ---------------------------------------------------------------------- */
  392. static void bochs_user_framebuffer_destroy(struct drm_framebuffer *fb)
  393. {
  394. struct bochs_framebuffer *bochs_fb = to_bochs_framebuffer(fb);
  395. if (bochs_fb->obj)
  396. drm_gem_object_unreference_unlocked(bochs_fb->obj);
  397. drm_framebuffer_cleanup(fb);
  398. kfree(fb);
  399. }
  400. static const struct drm_framebuffer_funcs bochs_fb_funcs = {
  401. .destroy = bochs_user_framebuffer_destroy,
  402. };
  403. int bochs_framebuffer_init(struct drm_device *dev,
  404. struct bochs_framebuffer *gfb,
  405. struct drm_mode_fb_cmd2 *mode_cmd,
  406. struct drm_gem_object *obj)
  407. {
  408. int ret;
  409. drm_helper_mode_fill_fb_struct(&gfb->base, mode_cmd);
  410. gfb->obj = obj;
  411. ret = drm_framebuffer_init(dev, &gfb->base, &bochs_fb_funcs);
  412. if (ret) {
  413. DRM_ERROR("drm_framebuffer_init failed: %d\n", ret);
  414. return ret;
  415. }
  416. return 0;
  417. }
  418. static struct drm_framebuffer *
  419. bochs_user_framebuffer_create(struct drm_device *dev,
  420. struct drm_file *filp,
  421. struct drm_mode_fb_cmd2 *mode_cmd)
  422. {
  423. struct drm_gem_object *obj;
  424. struct bochs_framebuffer *bochs_fb;
  425. int ret;
  426. DRM_DEBUG_DRIVER("%dx%d, format %c%c%c%c\n",
  427. mode_cmd->width, mode_cmd->height,
  428. (mode_cmd->pixel_format) & 0xff,
  429. (mode_cmd->pixel_format >> 8) & 0xff,
  430. (mode_cmd->pixel_format >> 16) & 0xff,
  431. (mode_cmd->pixel_format >> 24) & 0xff);
  432. if (mode_cmd->pixel_format != DRM_FORMAT_XRGB8888)
  433. return ERR_PTR(-ENOENT);
  434. obj = drm_gem_object_lookup(dev, filp, mode_cmd->handles[0]);
  435. if (obj == NULL)
  436. return ERR_PTR(-ENOENT);
  437. bochs_fb = kzalloc(sizeof(*bochs_fb), GFP_KERNEL);
  438. if (!bochs_fb) {
  439. drm_gem_object_unreference_unlocked(obj);
  440. return ERR_PTR(-ENOMEM);
  441. }
  442. ret = bochs_framebuffer_init(dev, bochs_fb, mode_cmd, obj);
  443. if (ret) {
  444. drm_gem_object_unreference_unlocked(obj);
  445. kfree(bochs_fb);
  446. return ERR_PTR(ret);
  447. }
  448. return &bochs_fb->base;
  449. }
  450. const struct drm_mode_config_funcs bochs_mode_funcs = {
  451. .fb_create = bochs_user_framebuffer_create,
  452. };