msm_drv.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113
  1. /*
  2. * Copyright (C) 2013 Red Hat
  3. * Author: Rob Clark <robdclark@gmail.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 as published by
  7. * the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that 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, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include "msm_drv.h"
  18. #include "msm_gpu.h"
  19. #include "msm_kms.h"
  20. static void msm_fb_output_poll_changed(struct drm_device *dev)
  21. {
  22. #ifdef CONFIG_DRM_MSM_FBDEV
  23. struct msm_drm_private *priv = dev->dev_private;
  24. if (priv->fbdev)
  25. drm_fb_helper_hotplug_event(priv->fbdev);
  26. #endif
  27. }
  28. static const struct drm_mode_config_funcs mode_config_funcs = {
  29. .fb_create = msm_framebuffer_create,
  30. .output_poll_changed = msm_fb_output_poll_changed,
  31. .atomic_check = msm_atomic_check,
  32. .atomic_commit = msm_atomic_commit,
  33. };
  34. int msm_register_mmu(struct drm_device *dev, struct msm_mmu *mmu)
  35. {
  36. struct msm_drm_private *priv = dev->dev_private;
  37. int idx = priv->num_mmus++;
  38. if (WARN_ON(idx >= ARRAY_SIZE(priv->mmus)))
  39. return -EINVAL;
  40. priv->mmus[idx] = mmu;
  41. return idx;
  42. }
  43. #ifdef CONFIG_DRM_MSM_REGISTER_LOGGING
  44. static bool reglog = false;
  45. MODULE_PARM_DESC(reglog, "Enable register read/write logging");
  46. module_param(reglog, bool, 0600);
  47. #else
  48. #define reglog 0
  49. #endif
  50. #ifdef CONFIG_DRM_MSM_FBDEV
  51. static bool fbdev = true;
  52. MODULE_PARM_DESC(fbdev, "Enable fbdev compat layer");
  53. module_param(fbdev, bool, 0600);
  54. #endif
  55. static char *vram = "16m";
  56. MODULE_PARM_DESC(vram, "Configure VRAM size (for devices without IOMMU/GPUMMU");
  57. module_param(vram, charp, 0);
  58. /*
  59. * Util/helpers:
  60. */
  61. void __iomem *msm_ioremap(struct platform_device *pdev, const char *name,
  62. const char *dbgname)
  63. {
  64. struct resource *res;
  65. unsigned long size;
  66. void __iomem *ptr;
  67. if (name)
  68. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, name);
  69. else
  70. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  71. if (!res) {
  72. dev_err(&pdev->dev, "failed to get memory resource: %s\n", name);
  73. return ERR_PTR(-EINVAL);
  74. }
  75. size = resource_size(res);
  76. ptr = devm_ioremap_nocache(&pdev->dev, res->start, size);
  77. if (!ptr) {
  78. dev_err(&pdev->dev, "failed to ioremap: %s\n", name);
  79. return ERR_PTR(-ENOMEM);
  80. }
  81. if (reglog)
  82. printk(KERN_DEBUG "IO:region %s %p %08lx\n", dbgname, ptr, size);
  83. return ptr;
  84. }
  85. void msm_writel(u32 data, void __iomem *addr)
  86. {
  87. if (reglog)
  88. printk(KERN_DEBUG "IO:W %p %08x\n", addr, data);
  89. writel(data, addr);
  90. }
  91. u32 msm_readl(const void __iomem *addr)
  92. {
  93. u32 val = readl(addr);
  94. if (reglog)
  95. printk(KERN_ERR "IO:R %p %08x\n", addr, val);
  96. return val;
  97. }
  98. /*
  99. * DRM operations:
  100. */
  101. static int msm_unload(struct drm_device *dev)
  102. {
  103. struct msm_drm_private *priv = dev->dev_private;
  104. struct msm_kms *kms = priv->kms;
  105. struct msm_gpu *gpu = priv->gpu;
  106. drm_kms_helper_poll_fini(dev);
  107. drm_mode_config_cleanup(dev);
  108. drm_vblank_cleanup(dev);
  109. pm_runtime_get_sync(dev->dev);
  110. drm_irq_uninstall(dev);
  111. pm_runtime_put_sync(dev->dev);
  112. flush_workqueue(priv->wq);
  113. destroy_workqueue(priv->wq);
  114. if (kms) {
  115. pm_runtime_disable(dev->dev);
  116. kms->funcs->destroy(kms);
  117. }
  118. if (gpu) {
  119. mutex_lock(&dev->struct_mutex);
  120. gpu->funcs->pm_suspend(gpu);
  121. mutex_unlock(&dev->struct_mutex);
  122. gpu->funcs->destroy(gpu);
  123. }
  124. if (priv->vram.paddr) {
  125. DEFINE_DMA_ATTRS(attrs);
  126. dma_set_attr(DMA_ATTR_NO_KERNEL_MAPPING, &attrs);
  127. drm_mm_takedown(&priv->vram.mm);
  128. dma_free_attrs(dev->dev, priv->vram.size, NULL,
  129. priv->vram.paddr, &attrs);
  130. }
  131. component_unbind_all(dev->dev, dev);
  132. dev->dev_private = NULL;
  133. kfree(priv);
  134. return 0;
  135. }
  136. static int get_mdp_ver(struct platform_device *pdev)
  137. {
  138. #ifdef CONFIG_OF
  139. static const struct of_device_id match_types[] = { {
  140. .compatible = "qcom,mdss_mdp",
  141. .data = (void *)5,
  142. }, {
  143. /* end node */
  144. } };
  145. struct device *dev = &pdev->dev;
  146. const struct of_device_id *match;
  147. match = of_match_node(match_types, dev->of_node);
  148. if (match)
  149. return (int)(unsigned long)match->data;
  150. #endif
  151. return 4;
  152. }
  153. #include <linux/of_address.h>
  154. static int msm_init_vram(struct drm_device *dev)
  155. {
  156. struct msm_drm_private *priv = dev->dev_private;
  157. unsigned long size = 0;
  158. int ret = 0;
  159. #ifdef CONFIG_OF
  160. /* In the device-tree world, we could have a 'memory-region'
  161. * phandle, which gives us a link to our "vram". Allocating
  162. * is all nicely abstracted behind the dma api, but we need
  163. * to know the entire size to allocate it all in one go. There
  164. * are two cases:
  165. * 1) device with no IOMMU, in which case we need exclusive
  166. * access to a VRAM carveout big enough for all gpu
  167. * buffers
  168. * 2) device with IOMMU, but where the bootloader puts up
  169. * a splash screen. In this case, the VRAM carveout
  170. * need only be large enough for fbdev fb. But we need
  171. * exclusive access to the buffer to avoid the kernel
  172. * using those pages for other purposes (which appears
  173. * as corruption on screen before we have a chance to
  174. * load and do initial modeset)
  175. */
  176. struct device_node *node;
  177. node = of_parse_phandle(dev->dev->of_node, "memory-region", 0);
  178. if (node) {
  179. struct resource r;
  180. ret = of_address_to_resource(node, 0, &r);
  181. if (ret)
  182. return ret;
  183. size = r.end - r.start;
  184. DRM_INFO("using VRAM carveout: %lx@%pa\n", size, &r.start);
  185. } else
  186. #endif
  187. /* if we have no IOMMU, then we need to use carveout allocator.
  188. * Grab the entire CMA chunk carved out in early startup in
  189. * mach-msm:
  190. */
  191. if (!iommu_present(&platform_bus_type)) {
  192. DRM_INFO("using %s VRAM carveout\n", vram);
  193. size = memparse(vram, NULL);
  194. }
  195. if (size) {
  196. DEFINE_DMA_ATTRS(attrs);
  197. void *p;
  198. priv->vram.size = size;
  199. drm_mm_init(&priv->vram.mm, 0, (size >> PAGE_SHIFT) - 1);
  200. dma_set_attr(DMA_ATTR_NO_KERNEL_MAPPING, &attrs);
  201. dma_set_attr(DMA_ATTR_WRITE_COMBINE, &attrs);
  202. /* note that for no-kernel-mapping, the vaddr returned
  203. * is bogus, but non-null if allocation succeeded:
  204. */
  205. p = dma_alloc_attrs(dev->dev, size,
  206. &priv->vram.paddr, GFP_KERNEL, &attrs);
  207. if (!p) {
  208. dev_err(dev->dev, "failed to allocate VRAM\n");
  209. priv->vram.paddr = 0;
  210. return -ENOMEM;
  211. }
  212. dev_info(dev->dev, "VRAM: %08x->%08x\n",
  213. (uint32_t)priv->vram.paddr,
  214. (uint32_t)(priv->vram.paddr + size));
  215. }
  216. return ret;
  217. }
  218. static int msm_load(struct drm_device *dev, unsigned long flags)
  219. {
  220. struct platform_device *pdev = dev->platformdev;
  221. struct msm_drm_private *priv;
  222. struct msm_kms *kms;
  223. int ret;
  224. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  225. if (!priv) {
  226. dev_err(dev->dev, "failed to allocate private data\n");
  227. return -ENOMEM;
  228. }
  229. dev->dev_private = priv;
  230. priv->wq = alloc_ordered_workqueue("msm", 0);
  231. init_waitqueue_head(&priv->fence_event);
  232. init_waitqueue_head(&priv->pending_crtcs_event);
  233. INIT_LIST_HEAD(&priv->inactive_list);
  234. INIT_LIST_HEAD(&priv->fence_cbs);
  235. drm_mode_config_init(dev);
  236. platform_set_drvdata(pdev, dev);
  237. /* Bind all our sub-components: */
  238. ret = component_bind_all(dev->dev, dev);
  239. if (ret)
  240. return ret;
  241. ret = msm_init_vram(dev);
  242. if (ret)
  243. goto fail;
  244. switch (get_mdp_ver(pdev)) {
  245. case 4:
  246. kms = mdp4_kms_init(dev);
  247. break;
  248. case 5:
  249. kms = mdp5_kms_init(dev);
  250. break;
  251. default:
  252. kms = ERR_PTR(-ENODEV);
  253. break;
  254. }
  255. if (IS_ERR(kms)) {
  256. /*
  257. * NOTE: once we have GPU support, having no kms should not
  258. * be considered fatal.. ideally we would still support gpu
  259. * and (for example) use dmabuf/prime to share buffers with
  260. * imx drm driver on iMX5
  261. */
  262. dev_err(dev->dev, "failed to load kms\n");
  263. ret = PTR_ERR(kms);
  264. goto fail;
  265. }
  266. priv->kms = kms;
  267. if (kms) {
  268. pm_runtime_enable(dev->dev);
  269. ret = kms->funcs->hw_init(kms);
  270. if (ret) {
  271. dev_err(dev->dev, "kms hw init failed: %d\n", ret);
  272. goto fail;
  273. }
  274. }
  275. dev->mode_config.min_width = 0;
  276. dev->mode_config.min_height = 0;
  277. dev->mode_config.max_width = 2048;
  278. dev->mode_config.max_height = 2048;
  279. dev->mode_config.funcs = &mode_config_funcs;
  280. ret = drm_vblank_init(dev, priv->num_crtcs);
  281. if (ret < 0) {
  282. dev_err(dev->dev, "failed to initialize vblank\n");
  283. goto fail;
  284. }
  285. pm_runtime_get_sync(dev->dev);
  286. ret = drm_irq_install(dev, platform_get_irq(dev->platformdev, 0));
  287. pm_runtime_put_sync(dev->dev);
  288. if (ret < 0) {
  289. dev_err(dev->dev, "failed to install IRQ handler\n");
  290. goto fail;
  291. }
  292. drm_mode_config_reset(dev);
  293. #ifdef CONFIG_DRM_MSM_FBDEV
  294. if (fbdev)
  295. priv->fbdev = msm_fbdev_init(dev);
  296. #endif
  297. ret = msm_debugfs_late_init(dev);
  298. if (ret)
  299. goto fail;
  300. drm_kms_helper_poll_init(dev);
  301. return 0;
  302. fail:
  303. msm_unload(dev);
  304. return ret;
  305. }
  306. static void load_gpu(struct drm_device *dev)
  307. {
  308. static DEFINE_MUTEX(init_lock);
  309. struct msm_drm_private *priv = dev->dev_private;
  310. mutex_lock(&init_lock);
  311. if (!priv->gpu)
  312. priv->gpu = adreno_load_gpu(dev);
  313. mutex_unlock(&init_lock);
  314. }
  315. static int msm_open(struct drm_device *dev, struct drm_file *file)
  316. {
  317. struct msm_file_private *ctx;
  318. /* For now, load gpu on open.. to avoid the requirement of having
  319. * firmware in the initrd.
  320. */
  321. load_gpu(dev);
  322. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  323. if (!ctx)
  324. return -ENOMEM;
  325. file->driver_priv = ctx;
  326. return 0;
  327. }
  328. static void msm_preclose(struct drm_device *dev, struct drm_file *file)
  329. {
  330. struct msm_drm_private *priv = dev->dev_private;
  331. struct msm_file_private *ctx = file->driver_priv;
  332. struct msm_kms *kms = priv->kms;
  333. if (kms)
  334. kms->funcs->preclose(kms, file);
  335. mutex_lock(&dev->struct_mutex);
  336. if (ctx == priv->lastctx)
  337. priv->lastctx = NULL;
  338. mutex_unlock(&dev->struct_mutex);
  339. kfree(ctx);
  340. }
  341. static void msm_lastclose(struct drm_device *dev)
  342. {
  343. #ifdef CONFIG_DRM_MSM_FBDEV
  344. struct msm_drm_private *priv = dev->dev_private;
  345. if (priv->fbdev)
  346. drm_fb_helper_restore_fbdev_mode_unlocked(priv->fbdev);
  347. #endif
  348. }
  349. static irqreturn_t msm_irq(int irq, void *arg)
  350. {
  351. struct drm_device *dev = arg;
  352. struct msm_drm_private *priv = dev->dev_private;
  353. struct msm_kms *kms = priv->kms;
  354. BUG_ON(!kms);
  355. return kms->funcs->irq(kms);
  356. }
  357. static void msm_irq_preinstall(struct drm_device *dev)
  358. {
  359. struct msm_drm_private *priv = dev->dev_private;
  360. struct msm_kms *kms = priv->kms;
  361. BUG_ON(!kms);
  362. kms->funcs->irq_preinstall(kms);
  363. }
  364. static int msm_irq_postinstall(struct drm_device *dev)
  365. {
  366. struct msm_drm_private *priv = dev->dev_private;
  367. struct msm_kms *kms = priv->kms;
  368. BUG_ON(!kms);
  369. return kms->funcs->irq_postinstall(kms);
  370. }
  371. static void msm_irq_uninstall(struct drm_device *dev)
  372. {
  373. struct msm_drm_private *priv = dev->dev_private;
  374. struct msm_kms *kms = priv->kms;
  375. BUG_ON(!kms);
  376. kms->funcs->irq_uninstall(kms);
  377. }
  378. static int msm_enable_vblank(struct drm_device *dev, int crtc_id)
  379. {
  380. struct msm_drm_private *priv = dev->dev_private;
  381. struct msm_kms *kms = priv->kms;
  382. if (!kms)
  383. return -ENXIO;
  384. DBG("dev=%p, crtc=%d", dev, crtc_id);
  385. return kms->funcs->enable_vblank(kms, priv->crtcs[crtc_id]);
  386. }
  387. static void msm_disable_vblank(struct drm_device *dev, int crtc_id)
  388. {
  389. struct msm_drm_private *priv = dev->dev_private;
  390. struct msm_kms *kms = priv->kms;
  391. if (!kms)
  392. return;
  393. DBG("dev=%p, crtc=%d", dev, crtc_id);
  394. kms->funcs->disable_vblank(kms, priv->crtcs[crtc_id]);
  395. }
  396. /*
  397. * DRM debugfs:
  398. */
  399. #ifdef CONFIG_DEBUG_FS
  400. static int msm_gpu_show(struct drm_device *dev, struct seq_file *m)
  401. {
  402. struct msm_drm_private *priv = dev->dev_private;
  403. struct msm_gpu *gpu = priv->gpu;
  404. if (gpu) {
  405. seq_printf(m, "%s Status:\n", gpu->name);
  406. gpu->funcs->show(gpu, m);
  407. }
  408. return 0;
  409. }
  410. static int msm_gem_show(struct drm_device *dev, struct seq_file *m)
  411. {
  412. struct msm_drm_private *priv = dev->dev_private;
  413. struct msm_gpu *gpu = priv->gpu;
  414. if (gpu) {
  415. seq_printf(m, "Active Objects (%s):\n", gpu->name);
  416. msm_gem_describe_objects(&gpu->active_list, m);
  417. }
  418. seq_printf(m, "Inactive Objects:\n");
  419. msm_gem_describe_objects(&priv->inactive_list, m);
  420. return 0;
  421. }
  422. static int msm_mm_show(struct drm_device *dev, struct seq_file *m)
  423. {
  424. return drm_mm_dump_table(m, &dev->vma_offset_manager->vm_addr_space_mm);
  425. }
  426. static int msm_fb_show(struct drm_device *dev, struct seq_file *m)
  427. {
  428. struct msm_drm_private *priv = dev->dev_private;
  429. struct drm_framebuffer *fb, *fbdev_fb = NULL;
  430. if (priv->fbdev) {
  431. seq_printf(m, "fbcon ");
  432. fbdev_fb = priv->fbdev->fb;
  433. msm_framebuffer_describe(fbdev_fb, m);
  434. }
  435. mutex_lock(&dev->mode_config.fb_lock);
  436. list_for_each_entry(fb, &dev->mode_config.fb_list, head) {
  437. if (fb == fbdev_fb)
  438. continue;
  439. seq_printf(m, "user ");
  440. msm_framebuffer_describe(fb, m);
  441. }
  442. mutex_unlock(&dev->mode_config.fb_lock);
  443. return 0;
  444. }
  445. static int show_locked(struct seq_file *m, void *arg)
  446. {
  447. struct drm_info_node *node = (struct drm_info_node *) m->private;
  448. struct drm_device *dev = node->minor->dev;
  449. int (*show)(struct drm_device *dev, struct seq_file *m) =
  450. node->info_ent->data;
  451. int ret;
  452. ret = mutex_lock_interruptible(&dev->struct_mutex);
  453. if (ret)
  454. return ret;
  455. ret = show(dev, m);
  456. mutex_unlock(&dev->struct_mutex);
  457. return ret;
  458. }
  459. static struct drm_info_list msm_debugfs_list[] = {
  460. {"gpu", show_locked, 0, msm_gpu_show},
  461. {"gem", show_locked, 0, msm_gem_show},
  462. { "mm", show_locked, 0, msm_mm_show },
  463. { "fb", show_locked, 0, msm_fb_show },
  464. };
  465. static int late_init_minor(struct drm_minor *minor)
  466. {
  467. int ret;
  468. if (!minor)
  469. return 0;
  470. ret = msm_rd_debugfs_init(minor);
  471. if (ret) {
  472. dev_err(minor->dev->dev, "could not install rd debugfs\n");
  473. return ret;
  474. }
  475. ret = msm_perf_debugfs_init(minor);
  476. if (ret) {
  477. dev_err(minor->dev->dev, "could not install perf debugfs\n");
  478. return ret;
  479. }
  480. return 0;
  481. }
  482. int msm_debugfs_late_init(struct drm_device *dev)
  483. {
  484. int ret;
  485. ret = late_init_minor(dev->primary);
  486. if (ret)
  487. return ret;
  488. ret = late_init_minor(dev->render);
  489. if (ret)
  490. return ret;
  491. ret = late_init_minor(dev->control);
  492. return ret;
  493. }
  494. static int msm_debugfs_init(struct drm_minor *minor)
  495. {
  496. struct drm_device *dev = minor->dev;
  497. int ret;
  498. ret = drm_debugfs_create_files(msm_debugfs_list,
  499. ARRAY_SIZE(msm_debugfs_list),
  500. minor->debugfs_root, minor);
  501. if (ret) {
  502. dev_err(dev->dev, "could not install msm_debugfs_list\n");
  503. return ret;
  504. }
  505. return 0;
  506. }
  507. static void msm_debugfs_cleanup(struct drm_minor *minor)
  508. {
  509. drm_debugfs_remove_files(msm_debugfs_list,
  510. ARRAY_SIZE(msm_debugfs_list), minor);
  511. if (!minor->dev->dev_private)
  512. return;
  513. msm_rd_debugfs_cleanup(minor);
  514. msm_perf_debugfs_cleanup(minor);
  515. }
  516. #endif
  517. /*
  518. * Fences:
  519. */
  520. int msm_wait_fence_interruptable(struct drm_device *dev, uint32_t fence,
  521. ktime_t *timeout)
  522. {
  523. struct msm_drm_private *priv = dev->dev_private;
  524. int ret;
  525. if (!priv->gpu)
  526. return 0;
  527. if (fence > priv->gpu->submitted_fence) {
  528. DRM_ERROR("waiting on invalid fence: %u (of %u)\n",
  529. fence, priv->gpu->submitted_fence);
  530. return -EINVAL;
  531. }
  532. if (!timeout) {
  533. /* no-wait: */
  534. ret = fence_completed(dev, fence) ? 0 : -EBUSY;
  535. } else {
  536. ktime_t now = ktime_get();
  537. unsigned long remaining_jiffies;
  538. if (ktime_compare(*timeout, now) < 0) {
  539. remaining_jiffies = 0;
  540. } else {
  541. ktime_t rem = ktime_sub(*timeout, now);
  542. struct timespec ts = ktime_to_timespec(rem);
  543. remaining_jiffies = timespec_to_jiffies(&ts);
  544. }
  545. ret = wait_event_interruptible_timeout(priv->fence_event,
  546. fence_completed(dev, fence),
  547. remaining_jiffies);
  548. if (ret == 0) {
  549. DBG("timeout waiting for fence: %u (completed: %u)",
  550. fence, priv->completed_fence);
  551. ret = -ETIMEDOUT;
  552. } else if (ret != -ERESTARTSYS) {
  553. ret = 0;
  554. }
  555. }
  556. return ret;
  557. }
  558. int msm_queue_fence_cb(struct drm_device *dev,
  559. struct msm_fence_cb *cb, uint32_t fence)
  560. {
  561. struct msm_drm_private *priv = dev->dev_private;
  562. int ret = 0;
  563. mutex_lock(&dev->struct_mutex);
  564. if (!list_empty(&cb->work.entry)) {
  565. ret = -EINVAL;
  566. } else if (fence > priv->completed_fence) {
  567. cb->fence = fence;
  568. list_add_tail(&cb->work.entry, &priv->fence_cbs);
  569. } else {
  570. queue_work(priv->wq, &cb->work);
  571. }
  572. mutex_unlock(&dev->struct_mutex);
  573. return ret;
  574. }
  575. /* called from workqueue */
  576. void msm_update_fence(struct drm_device *dev, uint32_t fence)
  577. {
  578. struct msm_drm_private *priv = dev->dev_private;
  579. mutex_lock(&dev->struct_mutex);
  580. priv->completed_fence = max(fence, priv->completed_fence);
  581. while (!list_empty(&priv->fence_cbs)) {
  582. struct msm_fence_cb *cb;
  583. cb = list_first_entry(&priv->fence_cbs,
  584. struct msm_fence_cb, work.entry);
  585. if (cb->fence > priv->completed_fence)
  586. break;
  587. list_del_init(&cb->work.entry);
  588. queue_work(priv->wq, &cb->work);
  589. }
  590. mutex_unlock(&dev->struct_mutex);
  591. wake_up_all(&priv->fence_event);
  592. }
  593. void __msm_fence_worker(struct work_struct *work)
  594. {
  595. struct msm_fence_cb *cb = container_of(work, struct msm_fence_cb, work);
  596. cb->func(cb);
  597. }
  598. /*
  599. * DRM ioctls:
  600. */
  601. static int msm_ioctl_get_param(struct drm_device *dev, void *data,
  602. struct drm_file *file)
  603. {
  604. struct msm_drm_private *priv = dev->dev_private;
  605. struct drm_msm_param *args = data;
  606. struct msm_gpu *gpu;
  607. /* for now, we just have 3d pipe.. eventually this would need to
  608. * be more clever to dispatch to appropriate gpu module:
  609. */
  610. if (args->pipe != MSM_PIPE_3D0)
  611. return -EINVAL;
  612. gpu = priv->gpu;
  613. if (!gpu)
  614. return -ENXIO;
  615. return gpu->funcs->get_param(gpu, args->param, &args->value);
  616. }
  617. static int msm_ioctl_gem_new(struct drm_device *dev, void *data,
  618. struct drm_file *file)
  619. {
  620. struct drm_msm_gem_new *args = data;
  621. if (args->flags & ~MSM_BO_FLAGS) {
  622. DRM_ERROR("invalid flags: %08x\n", args->flags);
  623. return -EINVAL;
  624. }
  625. return msm_gem_new_handle(dev, file, args->size,
  626. args->flags, &args->handle);
  627. }
  628. static inline ktime_t to_ktime(struct drm_msm_timespec timeout)
  629. {
  630. return ktime_set(timeout.tv_sec, timeout.tv_nsec);
  631. }
  632. static int msm_ioctl_gem_cpu_prep(struct drm_device *dev, void *data,
  633. struct drm_file *file)
  634. {
  635. struct drm_msm_gem_cpu_prep *args = data;
  636. struct drm_gem_object *obj;
  637. ktime_t timeout = to_ktime(args->timeout);
  638. int ret;
  639. if (args->op & ~MSM_PREP_FLAGS) {
  640. DRM_ERROR("invalid op: %08x\n", args->op);
  641. return -EINVAL;
  642. }
  643. obj = drm_gem_object_lookup(dev, file, args->handle);
  644. if (!obj)
  645. return -ENOENT;
  646. ret = msm_gem_cpu_prep(obj, args->op, &timeout);
  647. drm_gem_object_unreference_unlocked(obj);
  648. return ret;
  649. }
  650. static int msm_ioctl_gem_cpu_fini(struct drm_device *dev, void *data,
  651. struct drm_file *file)
  652. {
  653. struct drm_msm_gem_cpu_fini *args = data;
  654. struct drm_gem_object *obj;
  655. int ret;
  656. obj = drm_gem_object_lookup(dev, file, args->handle);
  657. if (!obj)
  658. return -ENOENT;
  659. ret = msm_gem_cpu_fini(obj);
  660. drm_gem_object_unreference_unlocked(obj);
  661. return ret;
  662. }
  663. static int msm_ioctl_gem_info(struct drm_device *dev, void *data,
  664. struct drm_file *file)
  665. {
  666. struct drm_msm_gem_info *args = data;
  667. struct drm_gem_object *obj;
  668. int ret = 0;
  669. if (args->pad)
  670. return -EINVAL;
  671. obj = drm_gem_object_lookup(dev, file, args->handle);
  672. if (!obj)
  673. return -ENOENT;
  674. args->offset = msm_gem_mmap_offset(obj);
  675. drm_gem_object_unreference_unlocked(obj);
  676. return ret;
  677. }
  678. static int msm_ioctl_wait_fence(struct drm_device *dev, void *data,
  679. struct drm_file *file)
  680. {
  681. struct drm_msm_wait_fence *args = data;
  682. ktime_t timeout = to_ktime(args->timeout);
  683. if (args->pad) {
  684. DRM_ERROR("invalid pad: %08x\n", args->pad);
  685. return -EINVAL;
  686. }
  687. return msm_wait_fence_interruptable(dev, args->fence, &timeout);
  688. }
  689. static const struct drm_ioctl_desc msm_ioctls[] = {
  690. DRM_IOCTL_DEF_DRV(MSM_GET_PARAM, msm_ioctl_get_param, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
  691. DRM_IOCTL_DEF_DRV(MSM_GEM_NEW, msm_ioctl_gem_new, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
  692. DRM_IOCTL_DEF_DRV(MSM_GEM_INFO, msm_ioctl_gem_info, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
  693. DRM_IOCTL_DEF_DRV(MSM_GEM_CPU_PREP, msm_ioctl_gem_cpu_prep, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
  694. DRM_IOCTL_DEF_DRV(MSM_GEM_CPU_FINI, msm_ioctl_gem_cpu_fini, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
  695. DRM_IOCTL_DEF_DRV(MSM_GEM_SUBMIT, msm_ioctl_gem_submit, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
  696. DRM_IOCTL_DEF_DRV(MSM_WAIT_FENCE, msm_ioctl_wait_fence, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
  697. };
  698. static const struct vm_operations_struct vm_ops = {
  699. .fault = msm_gem_fault,
  700. .open = drm_gem_vm_open,
  701. .close = drm_gem_vm_close,
  702. };
  703. static const struct file_operations fops = {
  704. .owner = THIS_MODULE,
  705. .open = drm_open,
  706. .release = drm_release,
  707. .unlocked_ioctl = drm_ioctl,
  708. #ifdef CONFIG_COMPAT
  709. .compat_ioctl = drm_compat_ioctl,
  710. #endif
  711. .poll = drm_poll,
  712. .read = drm_read,
  713. .llseek = no_llseek,
  714. .mmap = msm_gem_mmap,
  715. };
  716. static struct drm_driver msm_driver = {
  717. .driver_features = DRIVER_HAVE_IRQ |
  718. DRIVER_GEM |
  719. DRIVER_PRIME |
  720. DRIVER_RENDER |
  721. DRIVER_ATOMIC |
  722. DRIVER_MODESET,
  723. .load = msm_load,
  724. .unload = msm_unload,
  725. .open = msm_open,
  726. .preclose = msm_preclose,
  727. .lastclose = msm_lastclose,
  728. .set_busid = drm_platform_set_busid,
  729. .irq_handler = msm_irq,
  730. .irq_preinstall = msm_irq_preinstall,
  731. .irq_postinstall = msm_irq_postinstall,
  732. .irq_uninstall = msm_irq_uninstall,
  733. .get_vblank_counter = drm_vblank_count,
  734. .enable_vblank = msm_enable_vblank,
  735. .disable_vblank = msm_disable_vblank,
  736. .gem_free_object = msm_gem_free_object,
  737. .gem_vm_ops = &vm_ops,
  738. .dumb_create = msm_gem_dumb_create,
  739. .dumb_map_offset = msm_gem_dumb_map_offset,
  740. .dumb_destroy = drm_gem_dumb_destroy,
  741. .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
  742. .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
  743. .gem_prime_export = drm_gem_prime_export,
  744. .gem_prime_import = drm_gem_prime_import,
  745. .gem_prime_pin = msm_gem_prime_pin,
  746. .gem_prime_unpin = msm_gem_prime_unpin,
  747. .gem_prime_get_sg_table = msm_gem_prime_get_sg_table,
  748. .gem_prime_import_sg_table = msm_gem_prime_import_sg_table,
  749. .gem_prime_vmap = msm_gem_prime_vmap,
  750. .gem_prime_vunmap = msm_gem_prime_vunmap,
  751. .gem_prime_mmap = msm_gem_prime_mmap,
  752. #ifdef CONFIG_DEBUG_FS
  753. .debugfs_init = msm_debugfs_init,
  754. .debugfs_cleanup = msm_debugfs_cleanup,
  755. #endif
  756. .ioctls = msm_ioctls,
  757. .num_ioctls = DRM_MSM_NUM_IOCTLS,
  758. .fops = &fops,
  759. .name = "msm",
  760. .desc = "MSM Snapdragon DRM",
  761. .date = "20130625",
  762. .major = 1,
  763. .minor = 0,
  764. };
  765. #ifdef CONFIG_PM_SLEEP
  766. static int msm_pm_suspend(struct device *dev)
  767. {
  768. struct drm_device *ddev = dev_get_drvdata(dev);
  769. drm_kms_helper_poll_disable(ddev);
  770. return 0;
  771. }
  772. static int msm_pm_resume(struct device *dev)
  773. {
  774. struct drm_device *ddev = dev_get_drvdata(dev);
  775. drm_kms_helper_poll_enable(ddev);
  776. return 0;
  777. }
  778. #endif
  779. static const struct dev_pm_ops msm_pm_ops = {
  780. SET_SYSTEM_SLEEP_PM_OPS(msm_pm_suspend, msm_pm_resume)
  781. };
  782. /*
  783. * Componentized driver support:
  784. */
  785. #ifdef CONFIG_OF
  786. /* NOTE: the CONFIG_OF case duplicates the same code as exynos or imx
  787. * (or probably any other).. so probably some room for some helpers
  788. */
  789. static int compare_of(struct device *dev, void *data)
  790. {
  791. return dev->of_node == data;
  792. }
  793. static int add_components(struct device *dev, struct component_match **matchptr,
  794. const char *name)
  795. {
  796. struct device_node *np = dev->of_node;
  797. unsigned i;
  798. for (i = 0; ; i++) {
  799. struct device_node *node;
  800. node = of_parse_phandle(np, name, i);
  801. if (!node)
  802. break;
  803. component_match_add(dev, matchptr, compare_of, node);
  804. }
  805. return 0;
  806. }
  807. #else
  808. static int compare_dev(struct device *dev, void *data)
  809. {
  810. return dev == data;
  811. }
  812. #endif
  813. static int msm_drm_bind(struct device *dev)
  814. {
  815. return drm_platform_init(&msm_driver, to_platform_device(dev));
  816. }
  817. static void msm_drm_unbind(struct device *dev)
  818. {
  819. drm_put_dev(platform_get_drvdata(to_platform_device(dev)));
  820. }
  821. static const struct component_master_ops msm_drm_ops = {
  822. .bind = msm_drm_bind,
  823. .unbind = msm_drm_unbind,
  824. };
  825. /*
  826. * Platform driver:
  827. */
  828. static int msm_pdev_probe(struct platform_device *pdev)
  829. {
  830. struct component_match *match = NULL;
  831. #ifdef CONFIG_OF
  832. add_components(&pdev->dev, &match, "connectors");
  833. add_components(&pdev->dev, &match, "gpus");
  834. #else
  835. /* For non-DT case, it kinda sucks. We don't actually have a way
  836. * to know whether or not we are waiting for certain devices (or if
  837. * they are simply not present). But for non-DT we only need to
  838. * care about apq8064/apq8060/etc (all mdp4/a3xx):
  839. */
  840. static const char *devnames[] = {
  841. "hdmi_msm.0", "kgsl-3d0.0",
  842. };
  843. int i;
  844. DBG("Adding components..");
  845. for (i = 0; i < ARRAY_SIZE(devnames); i++) {
  846. struct device *dev;
  847. dev = bus_find_device_by_name(&platform_bus_type,
  848. NULL, devnames[i]);
  849. if (!dev) {
  850. dev_info(&pdev->dev, "still waiting for %s\n", devnames[i]);
  851. return -EPROBE_DEFER;
  852. }
  853. component_match_add(&pdev->dev, &match, compare_dev, dev);
  854. }
  855. #endif
  856. pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
  857. return component_master_add_with_match(&pdev->dev, &msm_drm_ops, match);
  858. }
  859. static int msm_pdev_remove(struct platform_device *pdev)
  860. {
  861. component_master_del(&pdev->dev, &msm_drm_ops);
  862. return 0;
  863. }
  864. static const struct platform_device_id msm_id[] = {
  865. { "mdp", 0 },
  866. { }
  867. };
  868. static const struct of_device_id dt_match[] = {
  869. { .compatible = "qcom,mdp" }, /* mdp4 */
  870. { .compatible = "qcom,mdss_mdp" }, /* mdp5 */
  871. {}
  872. };
  873. MODULE_DEVICE_TABLE(of, dt_match);
  874. static struct platform_driver msm_platform_driver = {
  875. .probe = msm_pdev_probe,
  876. .remove = msm_pdev_remove,
  877. .driver = {
  878. .name = "msm",
  879. .of_match_table = dt_match,
  880. .pm = &msm_pm_ops,
  881. },
  882. .id_table = msm_id,
  883. };
  884. static int __init msm_drm_register(void)
  885. {
  886. DBG("init");
  887. msm_dsi_register();
  888. msm_edp_register();
  889. hdmi_register();
  890. adreno_register();
  891. return platform_driver_register(&msm_platform_driver);
  892. }
  893. static void __exit msm_drm_unregister(void)
  894. {
  895. DBG("fini");
  896. platform_driver_unregister(&msm_platform_driver);
  897. hdmi_unregister();
  898. adreno_unregister();
  899. msm_edp_unregister();
  900. msm_dsi_unregister();
  901. }
  902. module_init(msm_drm_register);
  903. module_exit(msm_drm_unregister);
  904. MODULE_AUTHOR("Rob Clark <robdclark@gmail.com");
  905. MODULE_DESCRIPTION("MSM DRM Driver");
  906. MODULE_LICENSE("GPL");