drm.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  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/module.h>
  10. #include <linux/of_address.h>
  11. #include <linux/of_platform.h>
  12. #include <linux/dma-mapping.h>
  13. #include <asm/dma-iommu.h>
  14. #include <drm/drm.h>
  15. #include <drm/drmP.h>
  16. #include "host1x_client.h"
  17. #include "dev.h"
  18. #include "drm.h"
  19. #include "gem.h"
  20. #include "syncpt.h"
  21. #define DRIVER_NAME "tegra"
  22. #define DRIVER_DESC "NVIDIA Tegra graphics"
  23. #define DRIVER_DATE "20120330"
  24. #define DRIVER_MAJOR 0
  25. #define DRIVER_MINOR 0
  26. #define DRIVER_PATCHLEVEL 0
  27. struct tegra_drm_file {
  28. struct list_head contexts;
  29. };
  30. struct host1x_subdev {
  31. struct host1x_client *client;
  32. struct device_node *np;
  33. struct list_head list;
  34. };
  35. static int host1x_subdev_add(struct tegra_drm *tegra, struct device_node *np)
  36. {
  37. struct host1x_subdev *subdev;
  38. subdev = kzalloc(sizeof(*subdev), GFP_KERNEL);
  39. if (!subdev)
  40. return -ENOMEM;
  41. INIT_LIST_HEAD(&subdev->list);
  42. subdev->np = of_node_get(np);
  43. list_add_tail(&subdev->list, &tegra->subdevs);
  44. return 0;
  45. }
  46. static int host1x_subdev_register(struct tegra_drm *tegra,
  47. struct host1x_subdev *subdev,
  48. struct host1x_client *client)
  49. {
  50. mutex_lock(&tegra->subdevs_lock);
  51. list_del_init(&subdev->list);
  52. list_add_tail(&subdev->list, &tegra->active);
  53. subdev->client = client;
  54. mutex_unlock(&tegra->subdevs_lock);
  55. return 0;
  56. }
  57. static int host1x_subdev_unregister(struct tegra_drm *tegra,
  58. struct host1x_subdev *subdev)
  59. {
  60. mutex_lock(&tegra->subdevs_lock);
  61. list_del_init(&subdev->list);
  62. mutex_unlock(&tegra->subdevs_lock);
  63. of_node_put(subdev->np);
  64. kfree(subdev);
  65. return 0;
  66. }
  67. static int tegra_parse_dt(struct tegra_drm *tegra)
  68. {
  69. static const char * const compat[] = {
  70. "nvidia,tegra20-dc",
  71. "nvidia,tegra20-hdmi",
  72. "nvidia,tegra20-gr2d",
  73. "nvidia,tegra30-dc",
  74. "nvidia,tegra30-hdmi",
  75. "nvidia,tegra30-gr2d",
  76. };
  77. unsigned int i;
  78. int err;
  79. for (i = 0; i < ARRAY_SIZE(compat); i++) {
  80. struct device_node *np;
  81. for_each_child_of_node(tegra->dev->of_node, np) {
  82. if (of_device_is_compatible(np, compat[i]) &&
  83. of_device_is_available(np)) {
  84. err = host1x_subdev_add(tegra, np);
  85. if (err < 0)
  86. return err;
  87. }
  88. }
  89. }
  90. return 0;
  91. }
  92. int tegra_drm_alloc(struct platform_device *pdev)
  93. {
  94. struct tegra_drm *tegra;
  95. int err;
  96. tegra = devm_kzalloc(&pdev->dev, sizeof(*tegra), GFP_KERNEL);
  97. if (!tegra)
  98. return -ENOMEM;
  99. mutex_init(&tegra->subdevs_lock);
  100. INIT_LIST_HEAD(&tegra->subdevs);
  101. INIT_LIST_HEAD(&tegra->active);
  102. mutex_init(&tegra->clients_lock);
  103. INIT_LIST_HEAD(&tegra->clients);
  104. tegra->dev = &pdev->dev;
  105. err = tegra_parse_dt(tegra);
  106. if (err < 0) {
  107. dev_err(&pdev->dev, "failed to parse DT: %d\n", err);
  108. return err;
  109. }
  110. host1x_set_drm_data(&pdev->dev, tegra);
  111. return 0;
  112. }
  113. int tegra_drm_init(struct tegra_drm *tegra, struct drm_device *drm)
  114. {
  115. struct host1x_client *client;
  116. mutex_lock(&tegra->clients_lock);
  117. list_for_each_entry(client, &tegra->clients, list) {
  118. if (client->ops && client->ops->drm_init) {
  119. int err = client->ops->drm_init(client, drm);
  120. if (err < 0) {
  121. dev_err(tegra->dev,
  122. "DRM setup failed for %s: %d\n",
  123. dev_name(client->dev), err);
  124. mutex_unlock(&tegra->clients_lock);
  125. return err;
  126. }
  127. }
  128. }
  129. mutex_unlock(&tegra->clients_lock);
  130. return 0;
  131. }
  132. int tegra_drm_exit(struct tegra_drm *tegra)
  133. {
  134. struct platform_device *pdev = to_platform_device(tegra->dev);
  135. struct host1x_client *client;
  136. if (!tegra->drm)
  137. return 0;
  138. mutex_lock(&tegra->clients_lock);
  139. list_for_each_entry_reverse(client, &tegra->clients, list) {
  140. if (client->ops && client->ops->drm_exit) {
  141. int err = client->ops->drm_exit(client);
  142. if (err < 0) {
  143. dev_err(tegra->dev,
  144. "DRM cleanup failed for %s: %d\n",
  145. dev_name(client->dev), err);
  146. mutex_unlock(&tegra->clients_lock);
  147. return err;
  148. }
  149. }
  150. }
  151. mutex_unlock(&tegra->clients_lock);
  152. drm_platform_exit(&tegra_drm_driver, pdev);
  153. tegra->drm = NULL;
  154. return 0;
  155. }
  156. int host1x_register_client(struct tegra_drm *tegra,
  157. struct host1x_client *client)
  158. {
  159. struct host1x_subdev *subdev, *tmp;
  160. int err;
  161. mutex_lock(&tegra->clients_lock);
  162. list_add_tail(&client->list, &tegra->clients);
  163. mutex_unlock(&tegra->clients_lock);
  164. list_for_each_entry_safe(subdev, tmp, &tegra->subdevs, list)
  165. if (subdev->np == client->dev->of_node)
  166. host1x_subdev_register(tegra, subdev, client);
  167. if (list_empty(&tegra->subdevs)) {
  168. struct platform_device *pdev = to_platform_device(tegra->dev);
  169. err = drm_platform_init(&tegra_drm_driver, pdev);
  170. if (err < 0) {
  171. dev_err(tegra->dev, "drm_platform_init(): %d\n", err);
  172. return err;
  173. }
  174. }
  175. return 0;
  176. }
  177. int host1x_unregister_client(struct tegra_drm *tegra,
  178. struct host1x_client *client)
  179. {
  180. struct host1x_subdev *subdev, *tmp;
  181. int err;
  182. list_for_each_entry_safe(subdev, tmp, &tegra->active, list) {
  183. if (subdev->client == client) {
  184. err = tegra_drm_exit(tegra);
  185. if (err < 0) {
  186. dev_err(tegra->dev, "tegra_drm_exit(): %d\n",
  187. err);
  188. return err;
  189. }
  190. host1x_subdev_unregister(tegra, subdev);
  191. break;
  192. }
  193. }
  194. mutex_lock(&tegra->clients_lock);
  195. list_del_init(&client->list);
  196. mutex_unlock(&tegra->clients_lock);
  197. return 0;
  198. }
  199. static int tegra_drm_load(struct drm_device *drm, unsigned long flags)
  200. {
  201. struct tegra_drm *tegra;
  202. int err;
  203. tegra = host1x_get_drm_data(drm->dev);
  204. drm->dev_private = tegra;
  205. tegra->drm = drm;
  206. drm_mode_config_init(drm);
  207. err = tegra_drm_init(tegra, drm);
  208. if (err < 0)
  209. return err;
  210. /*
  211. * We don't use the drm_irq_install() helpers provided by the DRM
  212. * core, so we need to set this manually in order to allow the
  213. * DRM_IOCTL_WAIT_VBLANK to operate correctly.
  214. */
  215. drm->irq_enabled = true;
  216. err = drm_vblank_init(drm, drm->mode_config.num_crtc);
  217. if (err < 0)
  218. return err;
  219. err = tegra_drm_fb_init(drm);
  220. if (err < 0)
  221. return err;
  222. drm_kms_helper_poll_init(drm);
  223. return 0;
  224. }
  225. static int tegra_drm_unload(struct drm_device *drm)
  226. {
  227. drm_kms_helper_poll_fini(drm);
  228. tegra_drm_fb_exit(drm);
  229. drm_mode_config_cleanup(drm);
  230. return 0;
  231. }
  232. static int tegra_drm_open(struct drm_device *drm, struct drm_file *filp)
  233. {
  234. struct tegra_drm_file *fpriv;
  235. fpriv = kzalloc(sizeof(*fpriv), GFP_KERNEL);
  236. if (!fpriv)
  237. return -ENOMEM;
  238. INIT_LIST_HEAD(&fpriv->contexts);
  239. filp->driver_priv = fpriv;
  240. return 0;
  241. }
  242. static void host1x_drm_context_free(struct host1x_drm_context *context)
  243. {
  244. context->client->ops->close_channel(context);
  245. kfree(context);
  246. }
  247. static void tegra_drm_lastclose(struct drm_device *drm)
  248. {
  249. struct tegra_drm *tegra = drm->dev_private;
  250. tegra_fbdev_restore_mode(tegra->fbdev);
  251. }
  252. #ifdef CONFIG_DRM_TEGRA_STAGING
  253. static bool tegra_drm_file_owns_context(struct tegra_drm_file *file,
  254. struct host1x_drm_context *context)
  255. {
  256. struct host1x_drm_context *ctx;
  257. list_for_each_entry(ctx, &file->contexts, list)
  258. if (ctx == context)
  259. return true;
  260. return false;
  261. }
  262. static int tegra_gem_create(struct drm_device *drm, void *data,
  263. struct drm_file *file)
  264. {
  265. struct drm_tegra_gem_create *args = data;
  266. struct tegra_bo *bo;
  267. bo = tegra_bo_create_with_handle(file, drm, args->size,
  268. &args->handle);
  269. if (IS_ERR(bo))
  270. return PTR_ERR(bo);
  271. return 0;
  272. }
  273. static int tegra_gem_mmap(struct drm_device *drm, void *data,
  274. struct drm_file *file)
  275. {
  276. struct drm_tegra_gem_mmap *args = data;
  277. struct drm_gem_object *gem;
  278. struct tegra_bo *bo;
  279. gem = drm_gem_object_lookup(drm, file, args->handle);
  280. if (!gem)
  281. return -EINVAL;
  282. bo = to_tegra_bo(gem);
  283. args->offset = drm_vma_node_offset_addr(&bo->gem.vma_node);
  284. drm_gem_object_unreference(gem);
  285. return 0;
  286. }
  287. static int tegra_syncpt_read(struct drm_device *drm, void *data,
  288. struct drm_file *file)
  289. {
  290. struct drm_tegra_syncpt_read *args = data;
  291. struct host1x *host = dev_get_drvdata(drm->dev);
  292. struct host1x_syncpt *sp = host1x_syncpt_get(host, args->id);
  293. if (!sp)
  294. return -EINVAL;
  295. args->value = host1x_syncpt_read_min(sp);
  296. return 0;
  297. }
  298. static int tegra_syncpt_incr(struct drm_device *drm, void *data,
  299. struct drm_file *file)
  300. {
  301. struct drm_tegra_syncpt_incr *args = data;
  302. struct host1x *host = dev_get_drvdata(drm->dev);
  303. struct host1x_syncpt *sp = host1x_syncpt_get(host, args->id);
  304. if (!sp)
  305. return -EINVAL;
  306. return host1x_syncpt_incr(sp);
  307. }
  308. static int tegra_syncpt_wait(struct drm_device *drm, void *data,
  309. struct drm_file *file)
  310. {
  311. struct drm_tegra_syncpt_wait *args = data;
  312. struct host1x *host = dev_get_drvdata(drm->dev);
  313. struct host1x_syncpt *sp = host1x_syncpt_get(host, args->id);
  314. if (!sp)
  315. return -EINVAL;
  316. return host1x_syncpt_wait(sp, args->thresh, args->timeout,
  317. &args->value);
  318. }
  319. static int tegra_open_channel(struct drm_device *drm, void *data,
  320. struct drm_file *file)
  321. {
  322. struct tegra_drm_file *fpriv = file->driver_priv;
  323. struct tegra_drm *tegra = drm->dev_private;
  324. struct drm_tegra_open_channel *args = data;
  325. struct host1x_drm_context *context;
  326. struct host1x_client *client;
  327. int err = -ENODEV;
  328. context = kzalloc(sizeof(*context), GFP_KERNEL);
  329. if (!context)
  330. return -ENOMEM;
  331. list_for_each_entry(client, &tegra->clients, list)
  332. if (client->class == args->client) {
  333. err = client->ops->open_channel(client, context);
  334. if (err)
  335. break;
  336. context->client = client;
  337. list_add(&context->list, &fpriv->contexts);
  338. args->context = (uintptr_t)context;
  339. return 0;
  340. }
  341. kfree(context);
  342. return err;
  343. }
  344. static int tegra_close_channel(struct drm_device *drm, void *data,
  345. struct drm_file *file)
  346. {
  347. struct drm_tegra_close_channel *args = data;
  348. struct tegra_drm_file *fpriv = file->driver_priv;
  349. struct host1x_drm_context *context =
  350. (struct host1x_drm_context *)(uintptr_t)args->context;
  351. if (!tegra_drm_file_owns_context(fpriv, context))
  352. return -EINVAL;
  353. list_del(&context->list);
  354. host1x_drm_context_free(context);
  355. return 0;
  356. }
  357. static int tegra_get_syncpt(struct drm_device *drm, void *data,
  358. struct drm_file *file)
  359. {
  360. struct tegra_drm_file *fpriv = file->driver_priv;
  361. struct drm_tegra_get_syncpt *args = data;
  362. struct host1x_drm_context *context =
  363. (struct host1x_drm_context *)(uintptr_t)args->context;
  364. struct host1x_syncpt *syncpt;
  365. if (!tegra_drm_file_owns_context(fpriv, context))
  366. return -ENODEV;
  367. if (args->index >= context->client->num_syncpts)
  368. return -EINVAL;
  369. syncpt = context->client->syncpts[args->index];
  370. args->id = host1x_syncpt_id(syncpt);
  371. return 0;
  372. }
  373. static int tegra_submit(struct drm_device *drm, void *data,
  374. struct drm_file *file)
  375. {
  376. struct tegra_drm_file *fpriv = file->driver_priv;
  377. struct drm_tegra_submit *args = data;
  378. struct host1x_drm_context *context =
  379. (struct host1x_drm_context *)(uintptr_t)args->context;
  380. if (!tegra_drm_file_owns_context(fpriv, context))
  381. return -ENODEV;
  382. return context->client->ops->submit(context, args, drm, file);
  383. }
  384. #endif
  385. static const struct drm_ioctl_desc tegra_drm_ioctls[] = {
  386. #ifdef CONFIG_DRM_TEGRA_STAGING
  387. DRM_IOCTL_DEF_DRV(TEGRA_GEM_CREATE, tegra_gem_create, DRM_UNLOCKED | DRM_AUTH),
  388. DRM_IOCTL_DEF_DRV(TEGRA_GEM_MMAP, tegra_gem_mmap, DRM_UNLOCKED),
  389. DRM_IOCTL_DEF_DRV(TEGRA_SYNCPT_READ, tegra_syncpt_read, DRM_UNLOCKED),
  390. DRM_IOCTL_DEF_DRV(TEGRA_SYNCPT_INCR, tegra_syncpt_incr, DRM_UNLOCKED),
  391. DRM_IOCTL_DEF_DRV(TEGRA_SYNCPT_WAIT, tegra_syncpt_wait, DRM_UNLOCKED),
  392. DRM_IOCTL_DEF_DRV(TEGRA_OPEN_CHANNEL, tegra_open_channel, DRM_UNLOCKED),
  393. DRM_IOCTL_DEF_DRV(TEGRA_CLOSE_CHANNEL, tegra_close_channel, DRM_UNLOCKED),
  394. DRM_IOCTL_DEF_DRV(TEGRA_GET_SYNCPT, tegra_get_syncpt, DRM_UNLOCKED),
  395. DRM_IOCTL_DEF_DRV(TEGRA_SUBMIT, tegra_submit, DRM_UNLOCKED),
  396. #endif
  397. };
  398. static const struct file_operations tegra_drm_fops = {
  399. .owner = THIS_MODULE,
  400. .open = drm_open,
  401. .release = drm_release,
  402. .unlocked_ioctl = drm_ioctl,
  403. .mmap = tegra_drm_mmap,
  404. .poll = drm_poll,
  405. .read = drm_read,
  406. #ifdef CONFIG_COMPAT
  407. .compat_ioctl = drm_compat_ioctl,
  408. #endif
  409. .llseek = noop_llseek,
  410. };
  411. static struct drm_crtc *tegra_crtc_from_pipe(struct drm_device *drm, int pipe)
  412. {
  413. struct drm_crtc *crtc;
  414. list_for_each_entry(crtc, &drm->mode_config.crtc_list, head) {
  415. struct tegra_dc *dc = to_tegra_dc(crtc);
  416. if (dc->pipe == pipe)
  417. return crtc;
  418. }
  419. return NULL;
  420. }
  421. static u32 tegra_drm_get_vblank_counter(struct drm_device *dev, int crtc)
  422. {
  423. /* TODO: implement real hardware counter using syncpoints */
  424. return drm_vblank_count(dev, crtc);
  425. }
  426. static int tegra_drm_enable_vblank(struct drm_device *drm, int pipe)
  427. {
  428. struct drm_crtc *crtc = tegra_crtc_from_pipe(drm, pipe);
  429. struct tegra_dc *dc = to_tegra_dc(crtc);
  430. if (!crtc)
  431. return -ENODEV;
  432. tegra_dc_enable_vblank(dc);
  433. return 0;
  434. }
  435. static void tegra_drm_disable_vblank(struct drm_device *drm, int pipe)
  436. {
  437. struct drm_crtc *crtc = tegra_crtc_from_pipe(drm, pipe);
  438. struct tegra_dc *dc = to_tegra_dc(crtc);
  439. if (crtc)
  440. tegra_dc_disable_vblank(dc);
  441. }
  442. static void tegra_drm_preclose(struct drm_device *drm, struct drm_file *file)
  443. {
  444. struct tegra_drm_file *fpriv = file->driver_priv;
  445. struct host1x_drm_context *context, *tmp;
  446. struct drm_crtc *crtc;
  447. list_for_each_entry(crtc, &drm->mode_config.crtc_list, head)
  448. tegra_dc_cancel_page_flip(crtc, file);
  449. list_for_each_entry_safe(context, tmp, &fpriv->contexts, list)
  450. host1x_drm_context_free(context);
  451. kfree(fpriv);
  452. }
  453. #ifdef CONFIG_DEBUG_FS
  454. static int tegra_debugfs_framebuffers(struct seq_file *s, void *data)
  455. {
  456. struct drm_info_node *node = (struct drm_info_node *)s->private;
  457. struct drm_device *drm = node->minor->dev;
  458. struct drm_framebuffer *fb;
  459. mutex_lock(&drm->mode_config.fb_lock);
  460. list_for_each_entry(fb, &drm->mode_config.fb_list, head) {
  461. seq_printf(s, "%3d: user size: %d x %d, depth %d, %d bpp, refcount %d\n",
  462. fb->base.id, fb->width, fb->height, fb->depth,
  463. fb->bits_per_pixel,
  464. atomic_read(&fb->refcount.refcount));
  465. }
  466. mutex_unlock(&drm->mode_config.fb_lock);
  467. return 0;
  468. }
  469. static struct drm_info_list tegra_debugfs_list[] = {
  470. { "framebuffers", tegra_debugfs_framebuffers, 0 },
  471. };
  472. static int tegra_debugfs_init(struct drm_minor *minor)
  473. {
  474. return drm_debugfs_create_files(tegra_debugfs_list,
  475. ARRAY_SIZE(tegra_debugfs_list),
  476. minor->debugfs_root, minor);
  477. }
  478. static void tegra_debugfs_cleanup(struct drm_minor *minor)
  479. {
  480. drm_debugfs_remove_files(tegra_debugfs_list,
  481. ARRAY_SIZE(tegra_debugfs_list), minor);
  482. }
  483. #endif
  484. struct drm_driver tegra_drm_driver = {
  485. .driver_features = DRIVER_MODESET | DRIVER_GEM,
  486. .load = tegra_drm_load,
  487. .unload = tegra_drm_unload,
  488. .open = tegra_drm_open,
  489. .preclose = tegra_drm_preclose,
  490. .lastclose = tegra_drm_lastclose,
  491. .get_vblank_counter = tegra_drm_get_vblank_counter,
  492. .enable_vblank = tegra_drm_enable_vblank,
  493. .disable_vblank = tegra_drm_disable_vblank,
  494. #if defined(CONFIG_DEBUG_FS)
  495. .debugfs_init = tegra_debugfs_init,
  496. .debugfs_cleanup = tegra_debugfs_cleanup,
  497. #endif
  498. .gem_free_object = tegra_bo_free_object,
  499. .gem_vm_ops = &tegra_bo_vm_ops,
  500. .dumb_create = tegra_bo_dumb_create,
  501. .dumb_map_offset = tegra_bo_dumb_map_offset,
  502. .dumb_destroy = drm_gem_dumb_destroy,
  503. .ioctls = tegra_drm_ioctls,
  504. .num_ioctls = ARRAY_SIZE(tegra_drm_ioctls),
  505. .fops = &tegra_drm_fops,
  506. .name = DRIVER_NAME,
  507. .desc = DRIVER_DESC,
  508. .date = DRIVER_DATE,
  509. .major = DRIVER_MAJOR,
  510. .minor = DRIVER_MINOR,
  511. .patchlevel = DRIVER_PATCHLEVEL,
  512. };