drm.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150
  1. /*
  2. * Copyright (C) 2012 Avionic Design GmbH
  3. * Copyright (C) 2012-2013 NVIDIA CORPORATION. All rights reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. */
  9. #include <linux/host1x.h>
  10. #include <linux/iommu.h>
  11. #include <drm/drm_atomic.h>
  12. #include <drm/drm_atomic_helper.h>
  13. #include "drm.h"
  14. #include "gem.h"
  15. #define DRIVER_NAME "tegra"
  16. #define DRIVER_DESC "NVIDIA Tegra graphics"
  17. #define DRIVER_DATE "20120330"
  18. #define DRIVER_MAJOR 0
  19. #define DRIVER_MINOR 0
  20. #define DRIVER_PATCHLEVEL 0
  21. struct tegra_drm_file {
  22. struct list_head contexts;
  23. };
  24. static void tegra_atomic_schedule(struct tegra_drm *tegra,
  25. struct drm_atomic_state *state)
  26. {
  27. tegra->commit.state = state;
  28. schedule_work(&tegra->commit.work);
  29. }
  30. static void tegra_atomic_complete(struct tegra_drm *tegra,
  31. struct drm_atomic_state *state)
  32. {
  33. struct drm_device *drm = tegra->drm;
  34. /*
  35. * Everything below can be run asynchronously without the need to grab
  36. * any modeset locks at all under one condition: It must be guaranteed
  37. * that the asynchronous work has either been cancelled (if the driver
  38. * supports it, which at least requires that the framebuffers get
  39. * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
  40. * before the new state gets committed on the software side with
  41. * drm_atomic_helper_swap_state().
  42. *
  43. * This scheme allows new atomic state updates to be prepared and
  44. * checked in parallel to the asynchronous completion of the previous
  45. * update. Which is important since compositors need to figure out the
  46. * composition of the next frame right after having submitted the
  47. * current layout.
  48. */
  49. drm_atomic_helper_commit_modeset_disables(drm, state);
  50. drm_atomic_helper_commit_planes(drm, state, false);
  51. drm_atomic_helper_commit_modeset_enables(drm, state);
  52. drm_atomic_helper_wait_for_vblanks(drm, state);
  53. drm_atomic_helper_cleanup_planes(drm, state);
  54. drm_atomic_state_free(state);
  55. }
  56. static void tegra_atomic_work(struct work_struct *work)
  57. {
  58. struct tegra_drm *tegra = container_of(work, struct tegra_drm,
  59. commit.work);
  60. tegra_atomic_complete(tegra, tegra->commit.state);
  61. }
  62. static int tegra_atomic_commit(struct drm_device *drm,
  63. struct drm_atomic_state *state, bool async)
  64. {
  65. struct tegra_drm *tegra = drm->dev_private;
  66. int err;
  67. err = drm_atomic_helper_prepare_planes(drm, state);
  68. if (err)
  69. return err;
  70. /* serialize outstanding asynchronous commits */
  71. mutex_lock(&tegra->commit.lock);
  72. flush_work(&tegra->commit.work);
  73. /*
  74. * This is the point of no return - everything below never fails except
  75. * when the hw goes bonghits. Which means we can commit the new state on
  76. * the software side now.
  77. */
  78. drm_atomic_helper_swap_state(drm, state);
  79. if (async)
  80. tegra_atomic_schedule(tegra, state);
  81. else
  82. tegra_atomic_complete(tegra, state);
  83. mutex_unlock(&tegra->commit.lock);
  84. return 0;
  85. }
  86. static const struct drm_mode_config_funcs tegra_drm_mode_funcs = {
  87. .fb_create = tegra_fb_create,
  88. #ifdef CONFIG_DRM_TEGRA_FBDEV
  89. .output_poll_changed = tegra_fb_output_poll_changed,
  90. #endif
  91. .atomic_check = drm_atomic_helper_check,
  92. .atomic_commit = tegra_atomic_commit,
  93. };
  94. static int tegra_drm_load(struct drm_device *drm, unsigned long flags)
  95. {
  96. struct host1x_device *device = to_host1x_device(drm->dev);
  97. struct tegra_drm *tegra;
  98. int err;
  99. tegra = kzalloc(sizeof(*tegra), GFP_KERNEL);
  100. if (!tegra)
  101. return -ENOMEM;
  102. if (iommu_present(&platform_bus_type)) {
  103. struct iommu_domain_geometry *geometry;
  104. u64 start, end;
  105. tegra->domain = iommu_domain_alloc(&platform_bus_type);
  106. if (!tegra->domain) {
  107. err = -ENOMEM;
  108. goto free;
  109. }
  110. geometry = &tegra->domain->geometry;
  111. start = geometry->aperture_start;
  112. end = geometry->aperture_end;
  113. DRM_DEBUG("IOMMU context initialized (aperture: %#llx-%#llx)\n",
  114. start, end);
  115. drm_mm_init(&tegra->mm, start, end - start + 1);
  116. }
  117. mutex_init(&tegra->clients_lock);
  118. INIT_LIST_HEAD(&tegra->clients);
  119. mutex_init(&tegra->commit.lock);
  120. INIT_WORK(&tegra->commit.work, tegra_atomic_work);
  121. drm->dev_private = tegra;
  122. tegra->drm = drm;
  123. drm_mode_config_init(drm);
  124. drm->mode_config.min_width = 0;
  125. drm->mode_config.min_height = 0;
  126. drm->mode_config.max_width = 4096;
  127. drm->mode_config.max_height = 4096;
  128. drm->mode_config.funcs = &tegra_drm_mode_funcs;
  129. err = tegra_drm_fb_prepare(drm);
  130. if (err < 0)
  131. goto config;
  132. drm_kms_helper_poll_init(drm);
  133. err = host1x_device_init(device);
  134. if (err < 0)
  135. goto fbdev;
  136. /*
  137. * We don't use the drm_irq_install() helpers provided by the DRM
  138. * core, so we need to set this manually in order to allow the
  139. * DRM_IOCTL_WAIT_VBLANK to operate correctly.
  140. */
  141. drm->irq_enabled = true;
  142. /* syncpoints are used for full 32-bit hardware VBLANK counters */
  143. drm->max_vblank_count = 0xffffffff;
  144. drm->vblank_disable_allowed = true;
  145. err = drm_vblank_init(drm, drm->mode_config.num_crtc);
  146. if (err < 0)
  147. goto device;
  148. drm_mode_config_reset(drm);
  149. err = tegra_drm_fb_init(drm);
  150. if (err < 0)
  151. goto vblank;
  152. return 0;
  153. vblank:
  154. drm_vblank_cleanup(drm);
  155. device:
  156. host1x_device_exit(device);
  157. fbdev:
  158. drm_kms_helper_poll_fini(drm);
  159. tegra_drm_fb_free(drm);
  160. config:
  161. drm_mode_config_cleanup(drm);
  162. if (tegra->domain) {
  163. iommu_domain_free(tegra->domain);
  164. drm_mm_takedown(&tegra->mm);
  165. }
  166. free:
  167. kfree(tegra);
  168. return err;
  169. }
  170. static int tegra_drm_unload(struct drm_device *drm)
  171. {
  172. struct host1x_device *device = to_host1x_device(drm->dev);
  173. struct tegra_drm *tegra = drm->dev_private;
  174. int err;
  175. drm_kms_helper_poll_fini(drm);
  176. tegra_drm_fb_exit(drm);
  177. drm_mode_config_cleanup(drm);
  178. drm_vblank_cleanup(drm);
  179. err = host1x_device_exit(device);
  180. if (err < 0)
  181. return err;
  182. if (tegra->domain) {
  183. iommu_domain_free(tegra->domain);
  184. drm_mm_takedown(&tegra->mm);
  185. }
  186. kfree(tegra);
  187. return 0;
  188. }
  189. static int tegra_drm_open(struct drm_device *drm, struct drm_file *filp)
  190. {
  191. struct tegra_drm_file *fpriv;
  192. fpriv = kzalloc(sizeof(*fpriv), GFP_KERNEL);
  193. if (!fpriv)
  194. return -ENOMEM;
  195. INIT_LIST_HEAD(&fpriv->contexts);
  196. filp->driver_priv = fpriv;
  197. return 0;
  198. }
  199. static void tegra_drm_context_free(struct tegra_drm_context *context)
  200. {
  201. context->client->ops->close_channel(context);
  202. kfree(context);
  203. }
  204. static void tegra_drm_lastclose(struct drm_device *drm)
  205. {
  206. #ifdef CONFIG_DRM_TEGRA_FBDEV
  207. struct tegra_drm *tegra = drm->dev_private;
  208. tegra_fbdev_restore_mode(tegra->fbdev);
  209. #endif
  210. }
  211. static struct host1x_bo *
  212. host1x_bo_lookup(struct drm_device *drm, struct drm_file *file, u32 handle)
  213. {
  214. struct drm_gem_object *gem;
  215. struct tegra_bo *bo;
  216. gem = drm_gem_object_lookup(drm, file, handle);
  217. if (!gem)
  218. return NULL;
  219. mutex_lock(&drm->struct_mutex);
  220. drm_gem_object_unreference(gem);
  221. mutex_unlock(&drm->struct_mutex);
  222. bo = to_tegra_bo(gem);
  223. return &bo->base;
  224. }
  225. static int host1x_reloc_copy_from_user(struct host1x_reloc *dest,
  226. struct drm_tegra_reloc __user *src,
  227. struct drm_device *drm,
  228. struct drm_file *file)
  229. {
  230. u32 cmdbuf, target;
  231. int err;
  232. err = get_user(cmdbuf, &src->cmdbuf.handle);
  233. if (err < 0)
  234. return err;
  235. err = get_user(dest->cmdbuf.offset, &src->cmdbuf.offset);
  236. if (err < 0)
  237. return err;
  238. err = get_user(target, &src->target.handle);
  239. if (err < 0)
  240. return err;
  241. err = get_user(dest->target.offset, &src->target.offset);
  242. if (err < 0)
  243. return err;
  244. err = get_user(dest->shift, &src->shift);
  245. if (err < 0)
  246. return err;
  247. dest->cmdbuf.bo = host1x_bo_lookup(drm, file, cmdbuf);
  248. if (!dest->cmdbuf.bo)
  249. return -ENOENT;
  250. dest->target.bo = host1x_bo_lookup(drm, file, target);
  251. if (!dest->target.bo)
  252. return -ENOENT;
  253. return 0;
  254. }
  255. int tegra_drm_submit(struct tegra_drm_context *context,
  256. struct drm_tegra_submit *args, struct drm_device *drm,
  257. struct drm_file *file)
  258. {
  259. unsigned int num_cmdbufs = args->num_cmdbufs;
  260. unsigned int num_relocs = args->num_relocs;
  261. unsigned int num_waitchks = args->num_waitchks;
  262. struct drm_tegra_cmdbuf __user *cmdbufs =
  263. (void __user *)(uintptr_t)args->cmdbufs;
  264. struct drm_tegra_reloc __user *relocs =
  265. (void __user *)(uintptr_t)args->relocs;
  266. struct drm_tegra_waitchk __user *waitchks =
  267. (void __user *)(uintptr_t)args->waitchks;
  268. struct drm_tegra_syncpt syncpt;
  269. struct host1x_job *job;
  270. int err;
  271. /* We don't yet support other than one syncpt_incr struct per submit */
  272. if (args->num_syncpts != 1)
  273. return -EINVAL;
  274. job = host1x_job_alloc(context->channel, args->num_cmdbufs,
  275. args->num_relocs, args->num_waitchks);
  276. if (!job)
  277. return -ENOMEM;
  278. job->num_relocs = args->num_relocs;
  279. job->num_waitchk = args->num_waitchks;
  280. job->client = (u32)args->context;
  281. job->class = context->client->base.class;
  282. job->serialize = true;
  283. while (num_cmdbufs) {
  284. struct drm_tegra_cmdbuf cmdbuf;
  285. struct host1x_bo *bo;
  286. if (copy_from_user(&cmdbuf, cmdbufs, sizeof(cmdbuf))) {
  287. err = -EFAULT;
  288. goto fail;
  289. }
  290. bo = host1x_bo_lookup(drm, file, cmdbuf.handle);
  291. if (!bo) {
  292. err = -ENOENT;
  293. goto fail;
  294. }
  295. host1x_job_add_gather(job, bo, cmdbuf.words, cmdbuf.offset);
  296. num_cmdbufs--;
  297. cmdbufs++;
  298. }
  299. /* copy and resolve relocations from submit */
  300. while (num_relocs--) {
  301. err = host1x_reloc_copy_from_user(&job->relocarray[num_relocs],
  302. &relocs[num_relocs], drm,
  303. file);
  304. if (err < 0)
  305. goto fail;
  306. }
  307. if (copy_from_user(job->waitchk, waitchks,
  308. sizeof(*waitchks) * num_waitchks)) {
  309. err = -EFAULT;
  310. goto fail;
  311. }
  312. if (copy_from_user(&syncpt, (void __user *)(uintptr_t)args->syncpts,
  313. sizeof(syncpt))) {
  314. err = -EFAULT;
  315. goto fail;
  316. }
  317. job->is_addr_reg = context->client->ops->is_addr_reg;
  318. job->syncpt_incrs = syncpt.incrs;
  319. job->syncpt_id = syncpt.id;
  320. job->timeout = 10000;
  321. if (args->timeout && args->timeout < 10000)
  322. job->timeout = args->timeout;
  323. err = host1x_job_pin(job, context->client->base.dev);
  324. if (err)
  325. goto fail;
  326. err = host1x_job_submit(job);
  327. if (err)
  328. goto fail_submit;
  329. args->fence = job->syncpt_end;
  330. host1x_job_put(job);
  331. return 0;
  332. fail_submit:
  333. host1x_job_unpin(job);
  334. fail:
  335. host1x_job_put(job);
  336. return err;
  337. }
  338. #ifdef CONFIG_DRM_TEGRA_STAGING
  339. static struct tegra_drm_context *tegra_drm_get_context(__u64 context)
  340. {
  341. return (struct tegra_drm_context *)(uintptr_t)context;
  342. }
  343. static bool tegra_drm_file_owns_context(struct tegra_drm_file *file,
  344. struct tegra_drm_context *context)
  345. {
  346. struct tegra_drm_context *ctx;
  347. list_for_each_entry(ctx, &file->contexts, list)
  348. if (ctx == context)
  349. return true;
  350. return false;
  351. }
  352. static int tegra_gem_create(struct drm_device *drm, void *data,
  353. struct drm_file *file)
  354. {
  355. struct drm_tegra_gem_create *args = data;
  356. struct tegra_bo *bo;
  357. bo = tegra_bo_create_with_handle(file, drm, args->size, args->flags,
  358. &args->handle);
  359. if (IS_ERR(bo))
  360. return PTR_ERR(bo);
  361. return 0;
  362. }
  363. static int tegra_gem_mmap(struct drm_device *drm, void *data,
  364. struct drm_file *file)
  365. {
  366. struct drm_tegra_gem_mmap *args = data;
  367. struct drm_gem_object *gem;
  368. struct tegra_bo *bo;
  369. gem = drm_gem_object_lookup(drm, file, args->handle);
  370. if (!gem)
  371. return -EINVAL;
  372. bo = to_tegra_bo(gem);
  373. args->offset = drm_vma_node_offset_addr(&bo->gem.vma_node);
  374. drm_gem_object_unreference(gem);
  375. return 0;
  376. }
  377. static int tegra_syncpt_read(struct drm_device *drm, void *data,
  378. struct drm_file *file)
  379. {
  380. struct host1x *host = dev_get_drvdata(drm->dev->parent);
  381. struct drm_tegra_syncpt_read *args = data;
  382. struct host1x_syncpt *sp;
  383. sp = host1x_syncpt_get(host, args->id);
  384. if (!sp)
  385. return -EINVAL;
  386. args->value = host1x_syncpt_read_min(sp);
  387. return 0;
  388. }
  389. static int tegra_syncpt_incr(struct drm_device *drm, void *data,
  390. struct drm_file *file)
  391. {
  392. struct host1x *host1x = dev_get_drvdata(drm->dev->parent);
  393. struct drm_tegra_syncpt_incr *args = data;
  394. struct host1x_syncpt *sp;
  395. sp = host1x_syncpt_get(host1x, args->id);
  396. if (!sp)
  397. return -EINVAL;
  398. return host1x_syncpt_incr(sp);
  399. }
  400. static int tegra_syncpt_wait(struct drm_device *drm, void *data,
  401. struct drm_file *file)
  402. {
  403. struct host1x *host1x = dev_get_drvdata(drm->dev->parent);
  404. struct drm_tegra_syncpt_wait *args = data;
  405. struct host1x_syncpt *sp;
  406. sp = host1x_syncpt_get(host1x, args->id);
  407. if (!sp)
  408. return -EINVAL;
  409. return host1x_syncpt_wait(sp, args->thresh, args->timeout,
  410. &args->value);
  411. }
  412. static int tegra_open_channel(struct drm_device *drm, void *data,
  413. struct drm_file *file)
  414. {
  415. struct tegra_drm_file *fpriv = file->driver_priv;
  416. struct tegra_drm *tegra = drm->dev_private;
  417. struct drm_tegra_open_channel *args = data;
  418. struct tegra_drm_context *context;
  419. struct tegra_drm_client *client;
  420. int err = -ENODEV;
  421. context = kzalloc(sizeof(*context), GFP_KERNEL);
  422. if (!context)
  423. return -ENOMEM;
  424. list_for_each_entry(client, &tegra->clients, list)
  425. if (client->base.class == args->client) {
  426. err = client->ops->open_channel(client, context);
  427. if (err)
  428. break;
  429. list_add(&context->list, &fpriv->contexts);
  430. args->context = (uintptr_t)context;
  431. context->client = client;
  432. return 0;
  433. }
  434. kfree(context);
  435. return err;
  436. }
  437. static int tegra_close_channel(struct drm_device *drm, void *data,
  438. struct drm_file *file)
  439. {
  440. struct tegra_drm_file *fpriv = file->driver_priv;
  441. struct drm_tegra_close_channel *args = data;
  442. struct tegra_drm_context *context;
  443. context = tegra_drm_get_context(args->context);
  444. if (!tegra_drm_file_owns_context(fpriv, context))
  445. return -EINVAL;
  446. list_del(&context->list);
  447. tegra_drm_context_free(context);
  448. return 0;
  449. }
  450. static int tegra_get_syncpt(struct drm_device *drm, void *data,
  451. struct drm_file *file)
  452. {
  453. struct tegra_drm_file *fpriv = file->driver_priv;
  454. struct drm_tegra_get_syncpt *args = data;
  455. struct tegra_drm_context *context;
  456. struct host1x_syncpt *syncpt;
  457. context = tegra_drm_get_context(args->context);
  458. if (!tegra_drm_file_owns_context(fpriv, context))
  459. return -ENODEV;
  460. if (args->index >= context->client->base.num_syncpts)
  461. return -EINVAL;
  462. syncpt = context->client->base.syncpts[args->index];
  463. args->id = host1x_syncpt_id(syncpt);
  464. return 0;
  465. }
  466. static int tegra_submit(struct drm_device *drm, void *data,
  467. struct drm_file *file)
  468. {
  469. struct tegra_drm_file *fpriv = file->driver_priv;
  470. struct drm_tegra_submit *args = data;
  471. struct tegra_drm_context *context;
  472. context = tegra_drm_get_context(args->context);
  473. if (!tegra_drm_file_owns_context(fpriv, context))
  474. return -ENODEV;
  475. return context->client->ops->submit(context, args, drm, file);
  476. }
  477. static int tegra_get_syncpt_base(struct drm_device *drm, void *data,
  478. struct drm_file *file)
  479. {
  480. struct tegra_drm_file *fpriv = file->driver_priv;
  481. struct drm_tegra_get_syncpt_base *args = data;
  482. struct tegra_drm_context *context;
  483. struct host1x_syncpt_base *base;
  484. struct host1x_syncpt *syncpt;
  485. context = tegra_drm_get_context(args->context);
  486. if (!tegra_drm_file_owns_context(fpriv, context))
  487. return -ENODEV;
  488. if (args->syncpt >= context->client->base.num_syncpts)
  489. return -EINVAL;
  490. syncpt = context->client->base.syncpts[args->syncpt];
  491. base = host1x_syncpt_get_base(syncpt);
  492. if (!base)
  493. return -ENXIO;
  494. args->id = host1x_syncpt_base_id(base);
  495. return 0;
  496. }
  497. static int tegra_gem_set_tiling(struct drm_device *drm, void *data,
  498. struct drm_file *file)
  499. {
  500. struct drm_tegra_gem_set_tiling *args = data;
  501. enum tegra_bo_tiling_mode mode;
  502. struct drm_gem_object *gem;
  503. unsigned long value = 0;
  504. struct tegra_bo *bo;
  505. switch (args->mode) {
  506. case DRM_TEGRA_GEM_TILING_MODE_PITCH:
  507. mode = TEGRA_BO_TILING_MODE_PITCH;
  508. if (args->value != 0)
  509. return -EINVAL;
  510. break;
  511. case DRM_TEGRA_GEM_TILING_MODE_TILED:
  512. mode = TEGRA_BO_TILING_MODE_TILED;
  513. if (args->value != 0)
  514. return -EINVAL;
  515. break;
  516. case DRM_TEGRA_GEM_TILING_MODE_BLOCK:
  517. mode = TEGRA_BO_TILING_MODE_BLOCK;
  518. if (args->value > 5)
  519. return -EINVAL;
  520. value = args->value;
  521. break;
  522. default:
  523. return -EINVAL;
  524. }
  525. gem = drm_gem_object_lookup(drm, file, args->handle);
  526. if (!gem)
  527. return -ENOENT;
  528. bo = to_tegra_bo(gem);
  529. bo->tiling.mode = mode;
  530. bo->tiling.value = value;
  531. drm_gem_object_unreference(gem);
  532. return 0;
  533. }
  534. static int tegra_gem_get_tiling(struct drm_device *drm, void *data,
  535. struct drm_file *file)
  536. {
  537. struct drm_tegra_gem_get_tiling *args = data;
  538. struct drm_gem_object *gem;
  539. struct tegra_bo *bo;
  540. int err = 0;
  541. gem = drm_gem_object_lookup(drm, file, args->handle);
  542. if (!gem)
  543. return -ENOENT;
  544. bo = to_tegra_bo(gem);
  545. switch (bo->tiling.mode) {
  546. case TEGRA_BO_TILING_MODE_PITCH:
  547. args->mode = DRM_TEGRA_GEM_TILING_MODE_PITCH;
  548. args->value = 0;
  549. break;
  550. case TEGRA_BO_TILING_MODE_TILED:
  551. args->mode = DRM_TEGRA_GEM_TILING_MODE_TILED;
  552. args->value = 0;
  553. break;
  554. case TEGRA_BO_TILING_MODE_BLOCK:
  555. args->mode = DRM_TEGRA_GEM_TILING_MODE_BLOCK;
  556. args->value = bo->tiling.value;
  557. break;
  558. default:
  559. err = -EINVAL;
  560. break;
  561. }
  562. drm_gem_object_unreference(gem);
  563. return err;
  564. }
  565. static int tegra_gem_set_flags(struct drm_device *drm, void *data,
  566. struct drm_file *file)
  567. {
  568. struct drm_tegra_gem_set_flags *args = data;
  569. struct drm_gem_object *gem;
  570. struct tegra_bo *bo;
  571. if (args->flags & ~DRM_TEGRA_GEM_FLAGS)
  572. return -EINVAL;
  573. gem = drm_gem_object_lookup(drm, file, args->handle);
  574. if (!gem)
  575. return -ENOENT;
  576. bo = to_tegra_bo(gem);
  577. bo->flags = 0;
  578. if (args->flags & DRM_TEGRA_GEM_BOTTOM_UP)
  579. bo->flags |= TEGRA_BO_BOTTOM_UP;
  580. drm_gem_object_unreference(gem);
  581. return 0;
  582. }
  583. static int tegra_gem_get_flags(struct drm_device *drm, void *data,
  584. struct drm_file *file)
  585. {
  586. struct drm_tegra_gem_get_flags *args = data;
  587. struct drm_gem_object *gem;
  588. struct tegra_bo *bo;
  589. gem = drm_gem_object_lookup(drm, file, args->handle);
  590. if (!gem)
  591. return -ENOENT;
  592. bo = to_tegra_bo(gem);
  593. args->flags = 0;
  594. if (bo->flags & TEGRA_BO_BOTTOM_UP)
  595. args->flags |= DRM_TEGRA_GEM_BOTTOM_UP;
  596. drm_gem_object_unreference(gem);
  597. return 0;
  598. }
  599. #endif
  600. static const struct drm_ioctl_desc tegra_drm_ioctls[] = {
  601. #ifdef CONFIG_DRM_TEGRA_STAGING
  602. DRM_IOCTL_DEF_DRV(TEGRA_GEM_CREATE, tegra_gem_create, 0),
  603. DRM_IOCTL_DEF_DRV(TEGRA_GEM_MMAP, tegra_gem_mmap, 0),
  604. DRM_IOCTL_DEF_DRV(TEGRA_SYNCPT_READ, tegra_syncpt_read, 0),
  605. DRM_IOCTL_DEF_DRV(TEGRA_SYNCPT_INCR, tegra_syncpt_incr, 0),
  606. DRM_IOCTL_DEF_DRV(TEGRA_SYNCPT_WAIT, tegra_syncpt_wait, 0),
  607. DRM_IOCTL_DEF_DRV(TEGRA_OPEN_CHANNEL, tegra_open_channel, 0),
  608. DRM_IOCTL_DEF_DRV(TEGRA_CLOSE_CHANNEL, tegra_close_channel, 0),
  609. DRM_IOCTL_DEF_DRV(TEGRA_GET_SYNCPT, tegra_get_syncpt, 0),
  610. DRM_IOCTL_DEF_DRV(TEGRA_SUBMIT, tegra_submit, 0),
  611. DRM_IOCTL_DEF_DRV(TEGRA_GET_SYNCPT_BASE, tegra_get_syncpt_base, 0),
  612. DRM_IOCTL_DEF_DRV(TEGRA_GEM_SET_TILING, tegra_gem_set_tiling, 0),
  613. DRM_IOCTL_DEF_DRV(TEGRA_GEM_GET_TILING, tegra_gem_get_tiling, 0),
  614. DRM_IOCTL_DEF_DRV(TEGRA_GEM_SET_FLAGS, tegra_gem_set_flags, 0),
  615. DRM_IOCTL_DEF_DRV(TEGRA_GEM_GET_FLAGS, tegra_gem_get_flags, 0),
  616. #endif
  617. };
  618. static const struct file_operations tegra_drm_fops = {
  619. .owner = THIS_MODULE,
  620. .open = drm_open,
  621. .release = drm_release,
  622. .unlocked_ioctl = drm_ioctl,
  623. .mmap = tegra_drm_mmap,
  624. .poll = drm_poll,
  625. .read = drm_read,
  626. #ifdef CONFIG_COMPAT
  627. .compat_ioctl = drm_compat_ioctl,
  628. #endif
  629. .llseek = noop_llseek,
  630. };
  631. static struct drm_crtc *tegra_crtc_from_pipe(struct drm_device *drm,
  632. unsigned int pipe)
  633. {
  634. struct drm_crtc *crtc;
  635. list_for_each_entry(crtc, &drm->mode_config.crtc_list, head) {
  636. if (pipe == drm_crtc_index(crtc))
  637. return crtc;
  638. }
  639. return NULL;
  640. }
  641. static u32 tegra_drm_get_vblank_counter(struct drm_device *drm,
  642. unsigned int pipe)
  643. {
  644. struct drm_crtc *crtc = tegra_crtc_from_pipe(drm, pipe);
  645. struct tegra_dc *dc = to_tegra_dc(crtc);
  646. if (!crtc)
  647. return 0;
  648. return tegra_dc_get_vblank_counter(dc);
  649. }
  650. static int tegra_drm_enable_vblank(struct drm_device *drm, unsigned int pipe)
  651. {
  652. struct drm_crtc *crtc = tegra_crtc_from_pipe(drm, pipe);
  653. struct tegra_dc *dc = to_tegra_dc(crtc);
  654. if (!crtc)
  655. return -ENODEV;
  656. tegra_dc_enable_vblank(dc);
  657. return 0;
  658. }
  659. static void tegra_drm_disable_vblank(struct drm_device *drm, unsigned int pipe)
  660. {
  661. struct drm_crtc *crtc = tegra_crtc_from_pipe(drm, pipe);
  662. struct tegra_dc *dc = to_tegra_dc(crtc);
  663. if (crtc)
  664. tegra_dc_disable_vblank(dc);
  665. }
  666. static void tegra_drm_preclose(struct drm_device *drm, struct drm_file *file)
  667. {
  668. struct tegra_drm_file *fpriv = file->driver_priv;
  669. struct tegra_drm_context *context, *tmp;
  670. struct drm_crtc *crtc;
  671. list_for_each_entry(crtc, &drm->mode_config.crtc_list, head)
  672. tegra_dc_cancel_page_flip(crtc, file);
  673. list_for_each_entry_safe(context, tmp, &fpriv->contexts, list)
  674. tegra_drm_context_free(context);
  675. kfree(fpriv);
  676. }
  677. #ifdef CONFIG_DEBUG_FS
  678. static int tegra_debugfs_framebuffers(struct seq_file *s, void *data)
  679. {
  680. struct drm_info_node *node = (struct drm_info_node *)s->private;
  681. struct drm_device *drm = node->minor->dev;
  682. struct drm_framebuffer *fb;
  683. mutex_lock(&drm->mode_config.fb_lock);
  684. list_for_each_entry(fb, &drm->mode_config.fb_list, head) {
  685. seq_printf(s, "%3d: user size: %d x %d, depth %d, %d bpp, refcount %d\n",
  686. fb->base.id, fb->width, fb->height, fb->depth,
  687. fb->bits_per_pixel,
  688. atomic_read(&fb->refcount.refcount));
  689. }
  690. mutex_unlock(&drm->mode_config.fb_lock);
  691. return 0;
  692. }
  693. static int tegra_debugfs_iova(struct seq_file *s, void *data)
  694. {
  695. struct drm_info_node *node = (struct drm_info_node *)s->private;
  696. struct drm_device *drm = node->minor->dev;
  697. struct tegra_drm *tegra = drm->dev_private;
  698. return drm_mm_dump_table(s, &tegra->mm);
  699. }
  700. static struct drm_info_list tegra_debugfs_list[] = {
  701. { "framebuffers", tegra_debugfs_framebuffers, 0 },
  702. { "iova", tegra_debugfs_iova, 0 },
  703. };
  704. static int tegra_debugfs_init(struct drm_minor *minor)
  705. {
  706. return drm_debugfs_create_files(tegra_debugfs_list,
  707. ARRAY_SIZE(tegra_debugfs_list),
  708. minor->debugfs_root, minor);
  709. }
  710. static void tegra_debugfs_cleanup(struct drm_minor *minor)
  711. {
  712. drm_debugfs_remove_files(tegra_debugfs_list,
  713. ARRAY_SIZE(tegra_debugfs_list), minor);
  714. }
  715. #endif
  716. static struct drm_driver tegra_drm_driver = {
  717. .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_PRIME,
  718. .load = tegra_drm_load,
  719. .unload = tegra_drm_unload,
  720. .open = tegra_drm_open,
  721. .preclose = tegra_drm_preclose,
  722. .lastclose = tegra_drm_lastclose,
  723. .get_vblank_counter = tegra_drm_get_vblank_counter,
  724. .enable_vblank = tegra_drm_enable_vblank,
  725. .disable_vblank = tegra_drm_disable_vblank,
  726. #if defined(CONFIG_DEBUG_FS)
  727. .debugfs_init = tegra_debugfs_init,
  728. .debugfs_cleanup = tegra_debugfs_cleanup,
  729. #endif
  730. .gem_free_object = tegra_bo_free_object,
  731. .gem_vm_ops = &tegra_bo_vm_ops,
  732. .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
  733. .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
  734. .gem_prime_export = tegra_gem_prime_export,
  735. .gem_prime_import = tegra_gem_prime_import,
  736. .dumb_create = tegra_bo_dumb_create,
  737. .dumb_map_offset = tegra_bo_dumb_map_offset,
  738. .dumb_destroy = drm_gem_dumb_destroy,
  739. .ioctls = tegra_drm_ioctls,
  740. .num_ioctls = ARRAY_SIZE(tegra_drm_ioctls),
  741. .fops = &tegra_drm_fops,
  742. .name = DRIVER_NAME,
  743. .desc = DRIVER_DESC,
  744. .date = DRIVER_DATE,
  745. .major = DRIVER_MAJOR,
  746. .minor = DRIVER_MINOR,
  747. .patchlevel = DRIVER_PATCHLEVEL,
  748. };
  749. int tegra_drm_register_client(struct tegra_drm *tegra,
  750. struct tegra_drm_client *client)
  751. {
  752. mutex_lock(&tegra->clients_lock);
  753. list_add_tail(&client->list, &tegra->clients);
  754. mutex_unlock(&tegra->clients_lock);
  755. return 0;
  756. }
  757. int tegra_drm_unregister_client(struct tegra_drm *tegra,
  758. struct tegra_drm_client *client)
  759. {
  760. mutex_lock(&tegra->clients_lock);
  761. list_del_init(&client->list);
  762. mutex_unlock(&tegra->clients_lock);
  763. return 0;
  764. }
  765. static int host1x_drm_probe(struct host1x_device *dev)
  766. {
  767. struct drm_driver *driver = &tegra_drm_driver;
  768. struct drm_device *drm;
  769. int err;
  770. drm = drm_dev_alloc(driver, &dev->dev);
  771. if (!drm)
  772. return -ENOMEM;
  773. drm_dev_set_unique(drm, dev_name(&dev->dev));
  774. dev_set_drvdata(&dev->dev, drm);
  775. err = drm_dev_register(drm, 0);
  776. if (err < 0)
  777. goto unref;
  778. DRM_INFO("Initialized %s %d.%d.%d %s on minor %d\n", driver->name,
  779. driver->major, driver->minor, driver->patchlevel,
  780. driver->date, drm->primary->index);
  781. return 0;
  782. unref:
  783. drm_dev_unref(drm);
  784. return err;
  785. }
  786. static int host1x_drm_remove(struct host1x_device *dev)
  787. {
  788. struct drm_device *drm = dev_get_drvdata(&dev->dev);
  789. drm_dev_unregister(drm);
  790. drm_dev_unref(drm);
  791. return 0;
  792. }
  793. #ifdef CONFIG_PM_SLEEP
  794. static int host1x_drm_suspend(struct device *dev)
  795. {
  796. struct drm_device *drm = dev_get_drvdata(dev);
  797. drm_kms_helper_poll_disable(drm);
  798. return 0;
  799. }
  800. static int host1x_drm_resume(struct device *dev)
  801. {
  802. struct drm_device *drm = dev_get_drvdata(dev);
  803. drm_kms_helper_poll_enable(drm);
  804. return 0;
  805. }
  806. #endif
  807. static SIMPLE_DEV_PM_OPS(host1x_drm_pm_ops, host1x_drm_suspend,
  808. host1x_drm_resume);
  809. static const struct of_device_id host1x_drm_subdevs[] = {
  810. { .compatible = "nvidia,tegra20-dc", },
  811. { .compatible = "nvidia,tegra20-hdmi", },
  812. { .compatible = "nvidia,tegra20-gr2d", },
  813. { .compatible = "nvidia,tegra20-gr3d", },
  814. { .compatible = "nvidia,tegra30-dc", },
  815. { .compatible = "nvidia,tegra30-hdmi", },
  816. { .compatible = "nvidia,tegra30-gr2d", },
  817. { .compatible = "nvidia,tegra30-gr3d", },
  818. { .compatible = "nvidia,tegra114-dsi", },
  819. { .compatible = "nvidia,tegra114-hdmi", },
  820. { .compatible = "nvidia,tegra114-gr3d", },
  821. { .compatible = "nvidia,tegra124-dc", },
  822. { .compatible = "nvidia,tegra124-sor", },
  823. { .compatible = "nvidia,tegra124-hdmi", },
  824. { .compatible = "nvidia,tegra124-dsi", },
  825. { .compatible = "nvidia,tegra132-dsi", },
  826. { .compatible = "nvidia,tegra210-dc", },
  827. { .compatible = "nvidia,tegra210-dsi", },
  828. { .compatible = "nvidia,tegra210-sor", },
  829. { .compatible = "nvidia,tegra210-sor1", },
  830. { /* sentinel */ }
  831. };
  832. static struct host1x_driver host1x_drm_driver = {
  833. .driver = {
  834. .name = "drm",
  835. .pm = &host1x_drm_pm_ops,
  836. },
  837. .probe = host1x_drm_probe,
  838. .remove = host1x_drm_remove,
  839. .subdevs = host1x_drm_subdevs,
  840. };
  841. static int __init host1x_drm_init(void)
  842. {
  843. int err;
  844. err = host1x_driver_register(&host1x_drm_driver);
  845. if (err < 0)
  846. return err;
  847. err = platform_driver_register(&tegra_dc_driver);
  848. if (err < 0)
  849. goto unregister_host1x;
  850. err = platform_driver_register(&tegra_dsi_driver);
  851. if (err < 0)
  852. goto unregister_dc;
  853. err = platform_driver_register(&tegra_sor_driver);
  854. if (err < 0)
  855. goto unregister_dsi;
  856. err = platform_driver_register(&tegra_hdmi_driver);
  857. if (err < 0)
  858. goto unregister_sor;
  859. err = platform_driver_register(&tegra_dpaux_driver);
  860. if (err < 0)
  861. goto unregister_hdmi;
  862. err = platform_driver_register(&tegra_gr2d_driver);
  863. if (err < 0)
  864. goto unregister_dpaux;
  865. err = platform_driver_register(&tegra_gr3d_driver);
  866. if (err < 0)
  867. goto unregister_gr2d;
  868. return 0;
  869. unregister_gr2d:
  870. platform_driver_unregister(&tegra_gr2d_driver);
  871. unregister_dpaux:
  872. platform_driver_unregister(&tegra_dpaux_driver);
  873. unregister_hdmi:
  874. platform_driver_unregister(&tegra_hdmi_driver);
  875. unregister_sor:
  876. platform_driver_unregister(&tegra_sor_driver);
  877. unregister_dsi:
  878. platform_driver_unregister(&tegra_dsi_driver);
  879. unregister_dc:
  880. platform_driver_unregister(&tegra_dc_driver);
  881. unregister_host1x:
  882. host1x_driver_unregister(&host1x_drm_driver);
  883. return err;
  884. }
  885. module_init(host1x_drm_init);
  886. static void __exit host1x_drm_exit(void)
  887. {
  888. platform_driver_unregister(&tegra_gr3d_driver);
  889. platform_driver_unregister(&tegra_gr2d_driver);
  890. platform_driver_unregister(&tegra_dpaux_driver);
  891. platform_driver_unregister(&tegra_hdmi_driver);
  892. platform_driver_unregister(&tegra_sor_driver);
  893. platform_driver_unregister(&tegra_dsi_driver);
  894. platform_driver_unregister(&tegra_dc_driver);
  895. host1x_driver_unregister(&host1x_drm_driver);
  896. }
  897. module_exit(host1x_drm_exit);
  898. MODULE_AUTHOR("Thierry Reding <thierry.reding@avionic-design.de>");
  899. MODULE_DESCRIPTION("NVIDIA Tegra DRM driver");
  900. MODULE_LICENSE("GPL v2");