omap_drv.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2011 Texas Instruments Incorporated - https://www.ti.com/
  4. * Author: Rob Clark <[email protected]>
  5. */
  6. #include <linux/dma-mapping.h>
  7. #include <linux/platform_device.h>
  8. #include <linux/sort.h>
  9. #include <linux/sys_soc.h>
  10. #include <drm/drm_atomic.h>
  11. #include <drm/drm_atomic_helper.h>
  12. #include <drm/drm_bridge.h>
  13. #include <drm/drm_bridge_connector.h>
  14. #include <drm/drm_drv.h>
  15. #include <drm/drm_fb_helper.h>
  16. #include <drm/drm_file.h>
  17. #include <drm/drm_ioctl.h>
  18. #include <drm/drm_panel.h>
  19. #include <drm/drm_prime.h>
  20. #include <drm/drm_probe_helper.h>
  21. #include <drm/drm_vblank.h>
  22. #include "omap_dmm_tiler.h"
  23. #include "omap_drv.h"
  24. #define DRIVER_NAME MODULE_NAME
  25. #define DRIVER_DESC "OMAP DRM"
  26. #define DRIVER_DATE "20110917"
  27. #define DRIVER_MAJOR 1
  28. #define DRIVER_MINOR 0
  29. #define DRIVER_PATCHLEVEL 0
  30. /*
  31. * mode config funcs
  32. */
  33. /* Notes about mapping DSS and DRM entities:
  34. * CRTC: overlay
  35. * encoder: manager.. with some extension to allow one primary CRTC
  36. * and zero or more video CRTC's to be mapped to one encoder?
  37. * connector: dssdev.. manager can be attached/detached from different
  38. * devices
  39. */
  40. static void omap_atomic_wait_for_completion(struct drm_device *dev,
  41. struct drm_atomic_state *old_state)
  42. {
  43. struct drm_crtc_state *new_crtc_state;
  44. struct drm_crtc *crtc;
  45. unsigned int i;
  46. int ret;
  47. for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
  48. if (!new_crtc_state->active)
  49. continue;
  50. ret = omap_crtc_wait_pending(crtc);
  51. if (!ret)
  52. dev_warn(dev->dev,
  53. "atomic complete timeout (pipe %u)!\n", i);
  54. }
  55. }
  56. static void omap_atomic_commit_tail(struct drm_atomic_state *old_state)
  57. {
  58. struct drm_device *dev = old_state->dev;
  59. struct omap_drm_private *priv = dev->dev_private;
  60. bool fence_cookie = dma_fence_begin_signalling();
  61. dispc_runtime_get(priv->dispc);
  62. /* Apply the atomic update. */
  63. drm_atomic_helper_commit_modeset_disables(dev, old_state);
  64. if (priv->omaprev != 0x3430) {
  65. /* With the current dss dispc implementation we have to enable
  66. * the new modeset before we can commit planes. The dispc ovl
  67. * configuration relies on the video mode configuration been
  68. * written into the HW when the ovl configuration is
  69. * calculated.
  70. *
  71. * This approach is not ideal because after a mode change the
  72. * plane update is executed only after the first vblank
  73. * interrupt. The dispc implementation should be fixed so that
  74. * it is able use uncommitted drm state information.
  75. */
  76. drm_atomic_helper_commit_modeset_enables(dev, old_state);
  77. omap_atomic_wait_for_completion(dev, old_state);
  78. drm_atomic_helper_commit_planes(dev, old_state, 0);
  79. } else {
  80. /*
  81. * OMAP3 DSS seems to have issues with the work-around above,
  82. * resulting in endless sync losts if a crtc is enabled without
  83. * a plane. For now, skip the WA for OMAP3.
  84. */
  85. drm_atomic_helper_commit_planes(dev, old_state, 0);
  86. drm_atomic_helper_commit_modeset_enables(dev, old_state);
  87. }
  88. drm_atomic_helper_commit_hw_done(old_state);
  89. dma_fence_end_signalling(fence_cookie);
  90. /*
  91. * Wait for completion of the page flips to ensure that old buffers
  92. * can't be touched by the hardware anymore before cleaning up planes.
  93. */
  94. omap_atomic_wait_for_completion(dev, old_state);
  95. drm_atomic_helper_cleanup_planes(dev, old_state);
  96. dispc_runtime_put(priv->dispc);
  97. }
  98. static int drm_atomic_state_normalized_zpos_cmp(const void *a, const void *b)
  99. {
  100. const struct drm_plane_state *sa = *(struct drm_plane_state **)a;
  101. const struct drm_plane_state *sb = *(struct drm_plane_state **)b;
  102. if (sa->normalized_zpos != sb->normalized_zpos)
  103. return sa->normalized_zpos - sb->normalized_zpos;
  104. else
  105. return sa->plane->base.id - sb->plane->base.id;
  106. }
  107. /*
  108. * This replaces the drm_atomic_normalize_zpos to handle the dual overlay case.
  109. *
  110. * Since both halves need to be 'appear' side by side the zpos is
  111. * recalculated when dealing with dual overlay cases so that the other
  112. * planes zpos is consistent.
  113. */
  114. static int omap_atomic_update_normalize_zpos(struct drm_device *dev,
  115. struct drm_atomic_state *state)
  116. {
  117. struct drm_crtc *crtc;
  118. struct drm_crtc_state *old_state, *new_state;
  119. struct drm_plane *plane;
  120. int c, i, n, inc;
  121. int total_planes = dev->mode_config.num_total_plane;
  122. struct drm_plane_state **states;
  123. int ret = 0;
  124. states = kmalloc_array(total_planes, sizeof(*states), GFP_KERNEL);
  125. if (!states)
  126. return -ENOMEM;
  127. for_each_oldnew_crtc_in_state(state, crtc, old_state, new_state, c) {
  128. if (old_state->plane_mask == new_state->plane_mask &&
  129. !new_state->zpos_changed)
  130. continue;
  131. /* Reset plane increment and index value for every crtc */
  132. n = 0;
  133. /*
  134. * Normalization process might create new states for planes
  135. * which normalized_zpos has to be recalculated.
  136. */
  137. drm_for_each_plane_mask(plane, dev, new_state->plane_mask) {
  138. struct drm_plane_state *plane_state =
  139. drm_atomic_get_plane_state(new_state->state,
  140. plane);
  141. if (IS_ERR(plane_state)) {
  142. ret = PTR_ERR(plane_state);
  143. goto done;
  144. }
  145. states[n++] = plane_state;
  146. }
  147. sort(states, n, sizeof(*states),
  148. drm_atomic_state_normalized_zpos_cmp, NULL);
  149. for (i = 0, inc = 0; i < n; i++) {
  150. plane = states[i]->plane;
  151. states[i]->normalized_zpos = i + inc;
  152. DRM_DEBUG_ATOMIC("[PLANE:%d:%s] updated normalized zpos value %d\n",
  153. plane->base.id, plane->name,
  154. states[i]->normalized_zpos);
  155. if (is_omap_plane_dual_overlay(states[i]))
  156. inc++;
  157. }
  158. new_state->zpos_changed = true;
  159. }
  160. done:
  161. kfree(states);
  162. return ret;
  163. }
  164. static int omap_atomic_check(struct drm_device *dev,
  165. struct drm_atomic_state *state)
  166. {
  167. int ret;
  168. ret = drm_atomic_helper_check(dev, state);
  169. if (ret)
  170. return ret;
  171. if (dev->mode_config.normalize_zpos) {
  172. ret = omap_atomic_update_normalize_zpos(dev, state);
  173. if (ret)
  174. return ret;
  175. }
  176. return 0;
  177. }
  178. static const struct drm_mode_config_helper_funcs omap_mode_config_helper_funcs = {
  179. .atomic_commit_tail = omap_atomic_commit_tail,
  180. };
  181. static const struct drm_mode_config_funcs omap_mode_config_funcs = {
  182. .fb_create = omap_framebuffer_create,
  183. .output_poll_changed = drm_fb_helper_output_poll_changed,
  184. .atomic_check = omap_atomic_check,
  185. .atomic_commit = drm_atomic_helper_commit,
  186. };
  187. /* Global/shared object state funcs */
  188. /*
  189. * This is a helper that returns the private state currently in operation.
  190. * Note that this would return the "old_state" if called in the atomic check
  191. * path, and the "new_state" after the atomic swap has been done.
  192. */
  193. struct omap_global_state *
  194. omap_get_existing_global_state(struct omap_drm_private *priv)
  195. {
  196. return to_omap_global_state(priv->glob_obj.state);
  197. }
  198. /*
  199. * This acquires the modeset lock set aside for global state, creates
  200. * a new duplicated private object state.
  201. */
  202. struct omap_global_state *__must_check
  203. omap_get_global_state(struct drm_atomic_state *s)
  204. {
  205. struct omap_drm_private *priv = s->dev->dev_private;
  206. struct drm_private_state *priv_state;
  207. priv_state = drm_atomic_get_private_obj_state(s, &priv->glob_obj);
  208. if (IS_ERR(priv_state))
  209. return ERR_CAST(priv_state);
  210. return to_omap_global_state(priv_state);
  211. }
  212. static struct drm_private_state *
  213. omap_global_duplicate_state(struct drm_private_obj *obj)
  214. {
  215. struct omap_global_state *state;
  216. state = kmemdup(obj->state, sizeof(*state), GFP_KERNEL);
  217. if (!state)
  218. return NULL;
  219. __drm_atomic_helper_private_obj_duplicate_state(obj, &state->base);
  220. return &state->base;
  221. }
  222. static void omap_global_destroy_state(struct drm_private_obj *obj,
  223. struct drm_private_state *state)
  224. {
  225. struct omap_global_state *omap_state = to_omap_global_state(state);
  226. kfree(omap_state);
  227. }
  228. static const struct drm_private_state_funcs omap_global_state_funcs = {
  229. .atomic_duplicate_state = omap_global_duplicate_state,
  230. .atomic_destroy_state = omap_global_destroy_state,
  231. };
  232. static int omap_global_obj_init(struct drm_device *dev)
  233. {
  234. struct omap_drm_private *priv = dev->dev_private;
  235. struct omap_global_state *state;
  236. state = kzalloc(sizeof(*state), GFP_KERNEL);
  237. if (!state)
  238. return -ENOMEM;
  239. drm_atomic_private_obj_init(dev, &priv->glob_obj, &state->base,
  240. &omap_global_state_funcs);
  241. return 0;
  242. }
  243. static void omap_global_obj_fini(struct omap_drm_private *priv)
  244. {
  245. drm_atomic_private_obj_fini(&priv->glob_obj);
  246. }
  247. static void omap_disconnect_pipelines(struct drm_device *ddev)
  248. {
  249. struct omap_drm_private *priv = ddev->dev_private;
  250. unsigned int i;
  251. for (i = 0; i < priv->num_pipes; i++) {
  252. struct omap_drm_pipeline *pipe = &priv->pipes[i];
  253. omapdss_device_disconnect(NULL, pipe->output);
  254. omapdss_device_put(pipe->output);
  255. pipe->output = NULL;
  256. }
  257. memset(&priv->channels, 0, sizeof(priv->channels));
  258. priv->num_pipes = 0;
  259. }
  260. static int omap_connect_pipelines(struct drm_device *ddev)
  261. {
  262. struct omap_drm_private *priv = ddev->dev_private;
  263. struct omap_dss_device *output = NULL;
  264. int r;
  265. for_each_dss_output(output) {
  266. r = omapdss_device_connect(priv->dss, NULL, output);
  267. if (r == -EPROBE_DEFER) {
  268. omapdss_device_put(output);
  269. return r;
  270. } else if (r) {
  271. dev_warn(output->dev, "could not connect output %s\n",
  272. output->name);
  273. } else {
  274. struct omap_drm_pipeline *pipe;
  275. pipe = &priv->pipes[priv->num_pipes++];
  276. pipe->output = omapdss_device_get(output);
  277. if (priv->num_pipes == ARRAY_SIZE(priv->pipes)) {
  278. /* To balance the 'for_each_dss_output' loop */
  279. omapdss_device_put(output);
  280. break;
  281. }
  282. }
  283. }
  284. return 0;
  285. }
  286. static int omap_compare_pipelines(const void *a, const void *b)
  287. {
  288. const struct omap_drm_pipeline *pipe1 = a;
  289. const struct omap_drm_pipeline *pipe2 = b;
  290. if (pipe1->alias_id > pipe2->alias_id)
  291. return 1;
  292. else if (pipe1->alias_id < pipe2->alias_id)
  293. return -1;
  294. return 0;
  295. }
  296. static int omap_modeset_init_properties(struct drm_device *dev)
  297. {
  298. struct omap_drm_private *priv = dev->dev_private;
  299. unsigned int num_planes = dispc_get_num_ovls(priv->dispc);
  300. priv->zorder_prop = drm_property_create_range(dev, 0, "zorder", 0,
  301. num_planes - 1);
  302. if (!priv->zorder_prop)
  303. return -ENOMEM;
  304. return 0;
  305. }
  306. static int omap_display_id(struct omap_dss_device *output)
  307. {
  308. struct device_node *node = NULL;
  309. if (output->bridge) {
  310. struct drm_bridge *bridge = output->bridge;
  311. while (drm_bridge_get_next_bridge(bridge))
  312. bridge = drm_bridge_get_next_bridge(bridge);
  313. node = bridge->of_node;
  314. }
  315. return node ? of_alias_get_id(node, "display") : -ENODEV;
  316. }
  317. static int omap_modeset_init(struct drm_device *dev)
  318. {
  319. struct omap_drm_private *priv = dev->dev_private;
  320. int num_ovls = dispc_get_num_ovls(priv->dispc);
  321. int num_mgrs = dispc_get_num_mgrs(priv->dispc);
  322. unsigned int i;
  323. int ret;
  324. u32 plane_crtc_mask;
  325. if (!omapdss_stack_is_ready())
  326. return -EPROBE_DEFER;
  327. ret = omap_modeset_init_properties(dev);
  328. if (ret < 0)
  329. return ret;
  330. /*
  331. * This function creates exactly one connector, encoder, crtc,
  332. * and primary plane per each connected dss-device. Each
  333. * connector->encoder->crtc chain is expected to be separate
  334. * and each crtc is connect to a single dss-channel. If the
  335. * configuration does not match the expectations or exceeds
  336. * the available resources, the configuration is rejected.
  337. */
  338. ret = omap_connect_pipelines(dev);
  339. if (ret < 0)
  340. return ret;
  341. if (priv->num_pipes > num_mgrs || priv->num_pipes > num_ovls) {
  342. dev_err(dev->dev, "%s(): Too many connected displays\n",
  343. __func__);
  344. return -EINVAL;
  345. }
  346. /* Create all planes first. They can all be put to any CRTC. */
  347. plane_crtc_mask = (1 << priv->num_pipes) - 1;
  348. for (i = 0; i < num_ovls; i++) {
  349. enum drm_plane_type type = i < priv->num_pipes
  350. ? DRM_PLANE_TYPE_PRIMARY
  351. : DRM_PLANE_TYPE_OVERLAY;
  352. struct drm_plane *plane;
  353. if (WARN_ON(priv->num_planes >= ARRAY_SIZE(priv->planes)))
  354. return -EINVAL;
  355. plane = omap_plane_init(dev, i, type, plane_crtc_mask);
  356. if (IS_ERR(plane))
  357. return PTR_ERR(plane);
  358. priv->planes[priv->num_planes++] = plane;
  359. }
  360. /*
  361. * Create the encoders, attach the bridges and get the pipeline alias
  362. * IDs.
  363. */
  364. for (i = 0; i < priv->num_pipes; i++) {
  365. struct omap_drm_pipeline *pipe = &priv->pipes[i];
  366. int id;
  367. pipe->encoder = omap_encoder_init(dev, pipe->output);
  368. if (!pipe->encoder)
  369. return -ENOMEM;
  370. if (pipe->output->bridge) {
  371. ret = drm_bridge_attach(pipe->encoder,
  372. pipe->output->bridge, NULL,
  373. DRM_BRIDGE_ATTACH_NO_CONNECTOR);
  374. if (ret < 0)
  375. return ret;
  376. }
  377. id = omap_display_id(pipe->output);
  378. pipe->alias_id = id >= 0 ? id : i;
  379. }
  380. /* Sort the pipelines by DT aliases. */
  381. sort(priv->pipes, priv->num_pipes, sizeof(priv->pipes[0]),
  382. omap_compare_pipelines, NULL);
  383. /*
  384. * Populate the pipeline lookup table by DISPC channel. Only one display
  385. * is allowed per channel.
  386. */
  387. for (i = 0; i < priv->num_pipes; ++i) {
  388. struct omap_drm_pipeline *pipe = &priv->pipes[i];
  389. enum omap_channel channel = pipe->output->dispc_channel;
  390. if (WARN_ON(priv->channels[channel] != NULL))
  391. return -EINVAL;
  392. priv->channels[channel] = pipe;
  393. }
  394. /* Create the connectors and CRTCs. */
  395. for (i = 0; i < priv->num_pipes; i++) {
  396. struct omap_drm_pipeline *pipe = &priv->pipes[i];
  397. struct drm_encoder *encoder = pipe->encoder;
  398. struct drm_crtc *crtc;
  399. pipe->connector = drm_bridge_connector_init(dev, encoder);
  400. if (IS_ERR(pipe->connector)) {
  401. dev_err(priv->dev,
  402. "unable to create bridge connector for %s\n",
  403. pipe->output->name);
  404. return PTR_ERR(pipe->connector);
  405. }
  406. drm_connector_attach_encoder(pipe->connector, encoder);
  407. crtc = omap_crtc_init(dev, pipe, priv->planes[i]);
  408. if (IS_ERR(crtc))
  409. return PTR_ERR(crtc);
  410. encoder->possible_crtcs = 1 << i;
  411. pipe->crtc = crtc;
  412. }
  413. DBG("registered %u planes, %u crtcs/encoders/connectors\n",
  414. priv->num_planes, priv->num_pipes);
  415. dev->mode_config.min_width = 8;
  416. dev->mode_config.min_height = 2;
  417. /*
  418. * Note: these values are used for multiple independent things:
  419. * connector mode filtering, buffer sizes, crtc sizes...
  420. * Use big enough values here to cover all use cases, and do more
  421. * specific checking in the respective code paths.
  422. */
  423. dev->mode_config.max_width = 8192;
  424. dev->mode_config.max_height = 8192;
  425. /* We want the zpos to be normalized */
  426. dev->mode_config.normalize_zpos = true;
  427. dev->mode_config.funcs = &omap_mode_config_funcs;
  428. dev->mode_config.helper_private = &omap_mode_config_helper_funcs;
  429. drm_mode_config_reset(dev);
  430. omap_drm_irq_install(dev);
  431. return 0;
  432. }
  433. static void omap_modeset_fini(struct drm_device *ddev)
  434. {
  435. omap_drm_irq_uninstall(ddev);
  436. drm_mode_config_cleanup(ddev);
  437. }
  438. /*
  439. * Enable the HPD in external components if supported
  440. */
  441. static void omap_modeset_enable_external_hpd(struct drm_device *ddev)
  442. {
  443. struct omap_drm_private *priv = ddev->dev_private;
  444. unsigned int i;
  445. for (i = 0; i < priv->num_pipes; i++) {
  446. struct drm_connector *connector = priv->pipes[i].connector;
  447. if (!connector)
  448. continue;
  449. if (priv->pipes[i].output->bridge)
  450. drm_bridge_connector_enable_hpd(connector);
  451. }
  452. }
  453. /*
  454. * Disable the HPD in external components if supported
  455. */
  456. static void omap_modeset_disable_external_hpd(struct drm_device *ddev)
  457. {
  458. struct omap_drm_private *priv = ddev->dev_private;
  459. unsigned int i;
  460. for (i = 0; i < priv->num_pipes; i++) {
  461. struct drm_connector *connector = priv->pipes[i].connector;
  462. if (!connector)
  463. continue;
  464. if (priv->pipes[i].output->bridge)
  465. drm_bridge_connector_disable_hpd(connector);
  466. }
  467. }
  468. /*
  469. * drm ioctl funcs
  470. */
  471. static int ioctl_get_param(struct drm_device *dev, void *data,
  472. struct drm_file *file_priv)
  473. {
  474. struct omap_drm_private *priv = dev->dev_private;
  475. struct drm_omap_param *args = data;
  476. DBG("%p: param=%llu", dev, args->param);
  477. switch (args->param) {
  478. case OMAP_PARAM_CHIPSET_ID:
  479. args->value = priv->omaprev;
  480. break;
  481. default:
  482. DBG("unknown parameter %lld", args->param);
  483. return -EINVAL;
  484. }
  485. return 0;
  486. }
  487. #define OMAP_BO_USER_MASK 0x00ffffff /* flags settable by userspace */
  488. static int ioctl_gem_new(struct drm_device *dev, void *data,
  489. struct drm_file *file_priv)
  490. {
  491. struct drm_omap_gem_new *args = data;
  492. u32 flags = args->flags & OMAP_BO_USER_MASK;
  493. VERB("%p:%p: size=0x%08x, flags=%08x", dev, file_priv,
  494. args->size.bytes, flags);
  495. return omap_gem_new_handle(dev, file_priv, args->size, flags,
  496. &args->handle);
  497. }
  498. static int ioctl_gem_info(struct drm_device *dev, void *data,
  499. struct drm_file *file_priv)
  500. {
  501. struct drm_omap_gem_info *args = data;
  502. struct drm_gem_object *obj;
  503. int ret = 0;
  504. VERB("%p:%p: handle=%d", dev, file_priv, args->handle);
  505. obj = drm_gem_object_lookup(file_priv, args->handle);
  506. if (!obj)
  507. return -ENOENT;
  508. args->size = omap_gem_mmap_size(obj);
  509. args->offset = omap_gem_mmap_offset(obj);
  510. drm_gem_object_put(obj);
  511. return ret;
  512. }
  513. static const struct drm_ioctl_desc ioctls[DRM_COMMAND_END - DRM_COMMAND_BASE] = {
  514. DRM_IOCTL_DEF_DRV(OMAP_GET_PARAM, ioctl_get_param,
  515. DRM_RENDER_ALLOW),
  516. DRM_IOCTL_DEF_DRV(OMAP_SET_PARAM, drm_invalid_op,
  517. DRM_AUTH | DRM_MASTER | DRM_ROOT_ONLY),
  518. DRM_IOCTL_DEF_DRV(OMAP_GEM_NEW, ioctl_gem_new,
  519. DRM_RENDER_ALLOW),
  520. /* Deprecated, to be removed. */
  521. DRM_IOCTL_DEF_DRV(OMAP_GEM_CPU_PREP, drm_noop,
  522. DRM_RENDER_ALLOW),
  523. /* Deprecated, to be removed. */
  524. DRM_IOCTL_DEF_DRV(OMAP_GEM_CPU_FINI, drm_noop,
  525. DRM_RENDER_ALLOW),
  526. DRM_IOCTL_DEF_DRV(OMAP_GEM_INFO, ioctl_gem_info,
  527. DRM_RENDER_ALLOW),
  528. };
  529. /*
  530. * drm driver funcs
  531. */
  532. static int dev_open(struct drm_device *dev, struct drm_file *file)
  533. {
  534. file->driver_priv = NULL;
  535. DBG("open: dev=%p, file=%p", dev, file);
  536. return 0;
  537. }
  538. static const struct file_operations omapdriver_fops = {
  539. .owner = THIS_MODULE,
  540. .open = drm_open,
  541. .unlocked_ioctl = drm_ioctl,
  542. .compat_ioctl = drm_compat_ioctl,
  543. .release = drm_release,
  544. .mmap = omap_gem_mmap,
  545. .poll = drm_poll,
  546. .read = drm_read,
  547. .llseek = noop_llseek,
  548. };
  549. static const struct drm_driver omap_drm_driver = {
  550. .driver_features = DRIVER_MODESET | DRIVER_GEM |
  551. DRIVER_ATOMIC | DRIVER_RENDER,
  552. .open = dev_open,
  553. .lastclose = drm_fb_helper_lastclose,
  554. #ifdef CONFIG_DEBUG_FS
  555. .debugfs_init = omap_debugfs_init,
  556. #endif
  557. .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
  558. .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
  559. .gem_prime_import = omap_gem_prime_import,
  560. .dumb_create = omap_gem_dumb_create,
  561. .dumb_map_offset = omap_gem_dumb_map_offset,
  562. .ioctls = ioctls,
  563. .num_ioctls = DRM_OMAP_NUM_IOCTLS,
  564. .fops = &omapdriver_fops,
  565. .name = DRIVER_NAME,
  566. .desc = DRIVER_DESC,
  567. .date = DRIVER_DATE,
  568. .major = DRIVER_MAJOR,
  569. .minor = DRIVER_MINOR,
  570. .patchlevel = DRIVER_PATCHLEVEL,
  571. };
  572. static const struct soc_device_attribute omapdrm_soc_devices[] = {
  573. { .family = "OMAP3", .data = (void *)0x3430 },
  574. { .family = "OMAP4", .data = (void *)0x4430 },
  575. { .family = "OMAP5", .data = (void *)0x5430 },
  576. { .family = "DRA7", .data = (void *)0x0752 },
  577. { /* sentinel */ }
  578. };
  579. static int omapdrm_init(struct omap_drm_private *priv, struct device *dev)
  580. {
  581. const struct soc_device_attribute *soc;
  582. struct dss_pdata *pdata = dev->platform_data;
  583. struct drm_device *ddev;
  584. int ret;
  585. DBG("%s", dev_name(dev));
  586. if (drm_firmware_drivers_only())
  587. return -ENODEV;
  588. /* Allocate and initialize the DRM device. */
  589. ddev = drm_dev_alloc(&omap_drm_driver, dev);
  590. if (IS_ERR(ddev))
  591. return PTR_ERR(ddev);
  592. priv->ddev = ddev;
  593. ddev->dev_private = priv;
  594. priv->dev = dev;
  595. priv->dss = pdata->dss;
  596. priv->dispc = dispc_get_dispc(priv->dss);
  597. priv->dss->mgr_ops_priv = priv;
  598. soc = soc_device_match(omapdrm_soc_devices);
  599. priv->omaprev = soc ? (uintptr_t)soc->data : 0;
  600. priv->wq = alloc_ordered_workqueue("omapdrm", 0);
  601. mutex_init(&priv->list_lock);
  602. INIT_LIST_HEAD(&priv->obj_list);
  603. /* Get memory bandwidth limits */
  604. priv->max_bandwidth = dispc_get_memory_bandwidth_limit(priv->dispc);
  605. omap_gem_init(ddev);
  606. drm_mode_config_init(ddev);
  607. ret = omap_global_obj_init(ddev);
  608. if (ret)
  609. goto err_gem_deinit;
  610. ret = omap_hwoverlays_init(priv);
  611. if (ret)
  612. goto err_free_priv_obj;
  613. ret = omap_modeset_init(ddev);
  614. if (ret) {
  615. dev_err(priv->dev, "omap_modeset_init failed: ret=%d\n", ret);
  616. goto err_free_overlays;
  617. }
  618. /* Initialize vblank handling, start with all CRTCs disabled. */
  619. ret = drm_vblank_init(ddev, priv->num_pipes);
  620. if (ret) {
  621. dev_err(priv->dev, "could not init vblank\n");
  622. goto err_cleanup_modeset;
  623. }
  624. omap_fbdev_init(ddev);
  625. drm_kms_helper_poll_init(ddev);
  626. omap_modeset_enable_external_hpd(ddev);
  627. /*
  628. * Register the DRM device with the core and the connectors with
  629. * sysfs.
  630. */
  631. ret = drm_dev_register(ddev, 0);
  632. if (ret)
  633. goto err_cleanup_helpers;
  634. return 0;
  635. err_cleanup_helpers:
  636. omap_modeset_disable_external_hpd(ddev);
  637. drm_kms_helper_poll_fini(ddev);
  638. omap_fbdev_fini(ddev);
  639. err_cleanup_modeset:
  640. omap_modeset_fini(ddev);
  641. err_free_overlays:
  642. omap_hwoverlays_destroy(priv);
  643. err_free_priv_obj:
  644. omap_global_obj_fini(priv);
  645. err_gem_deinit:
  646. drm_mode_config_cleanup(ddev);
  647. omap_gem_deinit(ddev);
  648. destroy_workqueue(priv->wq);
  649. omap_disconnect_pipelines(ddev);
  650. drm_dev_put(ddev);
  651. return ret;
  652. }
  653. static void omapdrm_cleanup(struct omap_drm_private *priv)
  654. {
  655. struct drm_device *ddev = priv->ddev;
  656. DBG("");
  657. drm_dev_unregister(ddev);
  658. omap_modeset_disable_external_hpd(ddev);
  659. drm_kms_helper_poll_fini(ddev);
  660. omap_fbdev_fini(ddev);
  661. drm_atomic_helper_shutdown(ddev);
  662. omap_modeset_fini(ddev);
  663. omap_hwoverlays_destroy(priv);
  664. omap_global_obj_fini(priv);
  665. drm_mode_config_cleanup(ddev);
  666. omap_gem_deinit(ddev);
  667. destroy_workqueue(priv->wq);
  668. omap_disconnect_pipelines(ddev);
  669. drm_dev_put(ddev);
  670. }
  671. static int pdev_probe(struct platform_device *pdev)
  672. {
  673. struct omap_drm_private *priv;
  674. int ret;
  675. ret = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  676. if (ret) {
  677. dev_err(&pdev->dev, "Failed to set the DMA mask\n");
  678. return ret;
  679. }
  680. /* Allocate and initialize the driver private structure. */
  681. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  682. if (!priv)
  683. return -ENOMEM;
  684. platform_set_drvdata(pdev, priv);
  685. ret = omapdrm_init(priv, &pdev->dev);
  686. if (ret < 0)
  687. kfree(priv);
  688. return ret;
  689. }
  690. static int pdev_remove(struct platform_device *pdev)
  691. {
  692. struct omap_drm_private *priv = platform_get_drvdata(pdev);
  693. omapdrm_cleanup(priv);
  694. kfree(priv);
  695. return 0;
  696. }
  697. #ifdef CONFIG_PM_SLEEP
  698. static int omap_drm_suspend(struct device *dev)
  699. {
  700. struct omap_drm_private *priv = dev_get_drvdata(dev);
  701. struct drm_device *drm_dev = priv->ddev;
  702. return drm_mode_config_helper_suspend(drm_dev);
  703. }
  704. static int omap_drm_resume(struct device *dev)
  705. {
  706. struct omap_drm_private *priv = dev_get_drvdata(dev);
  707. struct drm_device *drm_dev = priv->ddev;
  708. drm_mode_config_helper_resume(drm_dev);
  709. return omap_gem_resume(drm_dev);
  710. }
  711. #endif
  712. static SIMPLE_DEV_PM_OPS(omapdrm_pm_ops, omap_drm_suspend, omap_drm_resume);
  713. static struct platform_driver pdev = {
  714. .driver = {
  715. .name = "omapdrm",
  716. .pm = &omapdrm_pm_ops,
  717. },
  718. .probe = pdev_probe,
  719. .remove = pdev_remove,
  720. };
  721. static struct platform_driver * const drivers[] = {
  722. &omap_dmm_driver,
  723. &pdev,
  724. };
  725. static int __init omap_drm_init(void)
  726. {
  727. int r;
  728. DBG("init");
  729. r = omap_dss_init();
  730. if (r)
  731. return r;
  732. r = platform_register_drivers(drivers, ARRAY_SIZE(drivers));
  733. if (r) {
  734. omap_dss_exit();
  735. return r;
  736. }
  737. return 0;
  738. }
  739. static void __exit omap_drm_fini(void)
  740. {
  741. DBG("fini");
  742. platform_unregister_drivers(drivers, ARRAY_SIZE(drivers));
  743. omap_dss_exit();
  744. }
  745. module_init(omap_drm_init);
  746. module_exit(omap_drm_fini);
  747. MODULE_AUTHOR("Rob Clark <[email protected]>");
  748. MODULE_AUTHOR("Tomi Valkeinen <[email protected]>");
  749. MODULE_DESCRIPTION("OMAP DRM Display Driver");
  750. MODULE_ALIAS("platform:" DRIVER_NAME);
  751. MODULE_LICENSE("GPL v2");