msm_drv.c 27 KB

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