msm_drv.c 26 KB

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