cirrus_mode.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. /*
  2. * Copyright 2012 Red Hat
  3. *
  4. * This file is subject to the terms and conditions of the GNU General
  5. * Public License version 2. See the file COPYING in the main
  6. * directory of this archive for more details.
  7. *
  8. * Authors: Matthew Garrett
  9. * Dave Airlie
  10. *
  11. * Portions of this code derived from cirrusfb.c:
  12. * drivers/video/cirrusfb.c - driver for Cirrus Logic chipsets
  13. *
  14. * Copyright 1999-2001 Jeff Garzik <jgarzik@pobox.com>
  15. */
  16. #include <drm/drmP.h>
  17. #include <drm/drm_crtc_helper.h>
  18. #include <drm/drm_plane_helper.h>
  19. #include <video/cirrus.h>
  20. #include "cirrus_drv.h"
  21. #define CIRRUS_LUT_SIZE 256
  22. #define PALETTE_INDEX 0x8
  23. #define PALETTE_DATA 0x9
  24. /*
  25. * This file contains setup code for the CRTC.
  26. */
  27. /*
  28. * The DRM core requires DPMS functions, but they make little sense in our
  29. * case and so are just stubs
  30. */
  31. static void cirrus_crtc_dpms(struct drm_crtc *crtc, int mode)
  32. {
  33. struct drm_device *dev = crtc->dev;
  34. struct cirrus_device *cdev = dev->dev_private;
  35. u8 sr01, gr0e;
  36. switch (mode) {
  37. case DRM_MODE_DPMS_ON:
  38. sr01 = 0x00;
  39. gr0e = 0x00;
  40. break;
  41. case DRM_MODE_DPMS_STANDBY:
  42. sr01 = 0x20;
  43. gr0e = 0x02;
  44. break;
  45. case DRM_MODE_DPMS_SUSPEND:
  46. sr01 = 0x20;
  47. gr0e = 0x04;
  48. break;
  49. case DRM_MODE_DPMS_OFF:
  50. sr01 = 0x20;
  51. gr0e = 0x06;
  52. break;
  53. default:
  54. return;
  55. }
  56. WREG8(SEQ_INDEX, 0x1);
  57. sr01 |= RREG8(SEQ_DATA) & ~0x20;
  58. WREG_SEQ(0x1, sr01);
  59. WREG8(GFX_INDEX, 0xe);
  60. gr0e |= RREG8(GFX_DATA) & ~0x06;
  61. WREG_GFX(0xe, gr0e);
  62. }
  63. static void cirrus_set_start_address(struct drm_crtc *crtc, unsigned offset)
  64. {
  65. struct cirrus_device *cdev = crtc->dev->dev_private;
  66. u32 addr;
  67. u8 tmp;
  68. addr = offset >> 2;
  69. WREG_CRT(0x0c, (u8)((addr >> 8) & 0xff));
  70. WREG_CRT(0x0d, (u8)(addr & 0xff));
  71. WREG8(CRT_INDEX, 0x1b);
  72. tmp = RREG8(CRT_DATA);
  73. tmp &= 0xf2;
  74. tmp |= (addr >> 16) & 0x01;
  75. tmp |= (addr >> 15) & 0x0c;
  76. WREG_CRT(0x1b, tmp);
  77. WREG8(CRT_INDEX, 0x1d);
  78. tmp = RREG8(CRT_DATA);
  79. tmp &= 0x7f;
  80. tmp |= (addr >> 12) & 0x80;
  81. WREG_CRT(0x1d, tmp);
  82. }
  83. /* cirrus is different - we will force move buffers out of VRAM */
  84. static int cirrus_crtc_do_set_base(struct drm_crtc *crtc,
  85. struct drm_framebuffer *fb,
  86. int x, int y, int atomic)
  87. {
  88. struct cirrus_device *cdev = crtc->dev->dev_private;
  89. struct cirrus_bo *bo;
  90. int ret;
  91. u64 gpu_addr;
  92. /* push the previous fb to system ram */
  93. if (!atomic && fb) {
  94. bo = gem_to_cirrus_bo(fb->obj[0]);
  95. ret = cirrus_bo_reserve(bo, false);
  96. if (ret)
  97. return ret;
  98. cirrus_bo_push_sysram(bo);
  99. cirrus_bo_unreserve(bo);
  100. }
  101. bo = gem_to_cirrus_bo(crtc->primary->fb->obj[0]);
  102. ret = cirrus_bo_reserve(bo, false);
  103. if (ret)
  104. return ret;
  105. ret = cirrus_bo_pin(bo, TTM_PL_FLAG_VRAM, &gpu_addr);
  106. if (ret) {
  107. cirrus_bo_unreserve(bo);
  108. return ret;
  109. }
  110. if (cdev->mode_info.gfbdev->gfb == crtc->primary->fb) {
  111. /* if pushing console in kmap it */
  112. ret = ttm_bo_kmap(&bo->bo, 0, bo->bo.num_pages, &bo->kmap);
  113. if (ret)
  114. DRM_ERROR("failed to kmap fbcon\n");
  115. }
  116. cirrus_bo_unreserve(bo);
  117. cirrus_set_start_address(crtc, (u32)gpu_addr);
  118. return 0;
  119. }
  120. static int cirrus_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
  121. struct drm_framebuffer *old_fb)
  122. {
  123. return cirrus_crtc_do_set_base(crtc, old_fb, x, y, 0);
  124. }
  125. /*
  126. * The meat of this driver. The core passes us a mode and we have to program
  127. * it. The modesetting here is the bare minimum required to satisfy the qemu
  128. * emulation of this hardware, and running this against a real device is
  129. * likely to result in an inadequately programmed mode. We've already had
  130. * the opportunity to modify the mode, so whatever we receive here should
  131. * be something that can be correctly programmed and displayed
  132. */
  133. static int cirrus_crtc_mode_set(struct drm_crtc *crtc,
  134. struct drm_display_mode *mode,
  135. struct drm_display_mode *adjusted_mode,
  136. int x, int y, struct drm_framebuffer *old_fb)
  137. {
  138. struct drm_device *dev = crtc->dev;
  139. struct cirrus_device *cdev = dev->dev_private;
  140. const struct drm_framebuffer *fb = crtc->primary->fb;
  141. int hsyncstart, hsyncend, htotal, hdispend;
  142. int vtotal, vdispend;
  143. int tmp;
  144. int sr07 = 0, hdr = 0;
  145. htotal = mode->htotal / 8;
  146. hsyncend = mode->hsync_end / 8;
  147. hsyncstart = mode->hsync_start / 8;
  148. hdispend = mode->hdisplay / 8;
  149. vtotal = mode->vtotal;
  150. vdispend = mode->vdisplay;
  151. vdispend -= 1;
  152. vtotal -= 2;
  153. htotal -= 5;
  154. hdispend -= 1;
  155. hsyncstart += 1;
  156. hsyncend += 1;
  157. WREG_CRT(VGA_CRTC_V_SYNC_END, 0x20);
  158. WREG_CRT(VGA_CRTC_H_TOTAL, htotal);
  159. WREG_CRT(VGA_CRTC_H_DISP, hdispend);
  160. WREG_CRT(VGA_CRTC_H_SYNC_START, hsyncstart);
  161. WREG_CRT(VGA_CRTC_H_SYNC_END, hsyncend);
  162. WREG_CRT(VGA_CRTC_V_TOTAL, vtotal & 0xff);
  163. WREG_CRT(VGA_CRTC_V_DISP_END, vdispend & 0xff);
  164. tmp = 0x40;
  165. if ((vdispend + 1) & 512)
  166. tmp |= 0x20;
  167. WREG_CRT(VGA_CRTC_MAX_SCAN, tmp);
  168. /*
  169. * Overflow bits for values that don't fit in the standard registers
  170. */
  171. tmp = 16;
  172. if (vtotal & 256)
  173. tmp |= 1;
  174. if (vdispend & 256)
  175. tmp |= 2;
  176. if ((vdispend + 1) & 256)
  177. tmp |= 8;
  178. if (vtotal & 512)
  179. tmp |= 32;
  180. if (vdispend & 512)
  181. tmp |= 64;
  182. WREG_CRT(VGA_CRTC_OVERFLOW, tmp);
  183. tmp = 0;
  184. /* More overflow bits */
  185. if ((htotal + 5) & 64)
  186. tmp |= 16;
  187. if ((htotal + 5) & 128)
  188. tmp |= 32;
  189. if (vtotal & 256)
  190. tmp |= 64;
  191. if (vtotal & 512)
  192. tmp |= 128;
  193. WREG_CRT(CL_CRT1A, tmp);
  194. /* Disable Hercules/CGA compatibility */
  195. WREG_CRT(VGA_CRTC_MODE, 0x03);
  196. WREG8(SEQ_INDEX, 0x7);
  197. sr07 = RREG8(SEQ_DATA);
  198. sr07 &= 0xe0;
  199. hdr = 0;
  200. switch (fb->format->cpp[0] * 8) {
  201. case 8:
  202. sr07 |= 0x11;
  203. break;
  204. case 16:
  205. sr07 |= 0x17;
  206. hdr = 0xc1;
  207. break;
  208. case 24:
  209. sr07 |= 0x15;
  210. hdr = 0xc5;
  211. break;
  212. case 32:
  213. sr07 |= 0x19;
  214. hdr = 0xc5;
  215. break;
  216. default:
  217. return -1;
  218. }
  219. WREG_SEQ(0x7, sr07);
  220. /* Program the pitch */
  221. tmp = fb->pitches[0] / 8;
  222. WREG_CRT(VGA_CRTC_OFFSET, tmp);
  223. /* Enable extended blanking and pitch bits, and enable full memory */
  224. tmp = 0x22;
  225. tmp |= (fb->pitches[0] >> 7) & 0x10;
  226. tmp |= (fb->pitches[0] >> 6) & 0x40;
  227. WREG_CRT(0x1b, tmp);
  228. /* Enable high-colour modes */
  229. WREG_GFX(VGA_GFX_MODE, 0x40);
  230. /* And set graphics mode */
  231. WREG_GFX(VGA_GFX_MISC, 0x01);
  232. WREG_HDR(hdr);
  233. cirrus_crtc_do_set_base(crtc, old_fb, x, y, 0);
  234. /* Unblank (needed on S3 resume, vgabios doesn't do it then) */
  235. outb(0x20, 0x3c0);
  236. return 0;
  237. }
  238. /*
  239. * This is called before a mode is programmed. A typical use might be to
  240. * enable DPMS during the programming to avoid seeing intermediate stages,
  241. * but that's not relevant to us
  242. */
  243. static void cirrus_crtc_prepare(struct drm_crtc *crtc)
  244. {
  245. }
  246. static void cirrus_crtc_load_lut(struct drm_crtc *crtc)
  247. {
  248. struct drm_device *dev = crtc->dev;
  249. struct cirrus_device *cdev = dev->dev_private;
  250. u16 *r, *g, *b;
  251. int i;
  252. if (!crtc->enabled)
  253. return;
  254. r = crtc->gamma_store;
  255. g = r + crtc->gamma_size;
  256. b = g + crtc->gamma_size;
  257. for (i = 0; i < CIRRUS_LUT_SIZE; i++) {
  258. /* VGA registers */
  259. WREG8(PALETTE_INDEX, i);
  260. WREG8(PALETTE_DATA, *r++ >> 8);
  261. WREG8(PALETTE_DATA, *g++ >> 8);
  262. WREG8(PALETTE_DATA, *b++ >> 8);
  263. }
  264. }
  265. /*
  266. * This is called after a mode is programmed. It should reverse anything done
  267. * by the prepare function
  268. */
  269. static void cirrus_crtc_commit(struct drm_crtc *crtc)
  270. {
  271. cirrus_crtc_load_lut(crtc);
  272. }
  273. /*
  274. * The core can pass us a set of gamma values to program. We actually only
  275. * use this for 8-bit mode so can't perform smooth fades on deeper modes,
  276. * but it's a requirement that we provide the function
  277. */
  278. static int cirrus_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
  279. u16 *blue, uint32_t size,
  280. struct drm_modeset_acquire_ctx *ctx)
  281. {
  282. cirrus_crtc_load_lut(crtc);
  283. return 0;
  284. }
  285. /* Simple cleanup function */
  286. static void cirrus_crtc_destroy(struct drm_crtc *crtc)
  287. {
  288. struct cirrus_crtc *cirrus_crtc = to_cirrus_crtc(crtc);
  289. drm_crtc_cleanup(crtc);
  290. kfree(cirrus_crtc);
  291. }
  292. /* These provide the minimum set of functions required to handle a CRTC */
  293. static const struct drm_crtc_funcs cirrus_crtc_funcs = {
  294. .gamma_set = cirrus_crtc_gamma_set,
  295. .set_config = drm_crtc_helper_set_config,
  296. .destroy = cirrus_crtc_destroy,
  297. };
  298. static const struct drm_crtc_helper_funcs cirrus_helper_funcs = {
  299. .dpms = cirrus_crtc_dpms,
  300. .mode_set = cirrus_crtc_mode_set,
  301. .mode_set_base = cirrus_crtc_mode_set_base,
  302. .prepare = cirrus_crtc_prepare,
  303. .commit = cirrus_crtc_commit,
  304. };
  305. /* CRTC setup */
  306. static void cirrus_crtc_init(struct drm_device *dev)
  307. {
  308. struct cirrus_device *cdev = dev->dev_private;
  309. struct cirrus_crtc *cirrus_crtc;
  310. cirrus_crtc = kzalloc(sizeof(struct cirrus_crtc) +
  311. (CIRRUSFB_CONN_LIMIT * sizeof(struct drm_connector *)),
  312. GFP_KERNEL);
  313. if (cirrus_crtc == NULL)
  314. return;
  315. drm_crtc_init(dev, &cirrus_crtc->base, &cirrus_crtc_funcs);
  316. drm_mode_crtc_set_gamma_size(&cirrus_crtc->base, CIRRUS_LUT_SIZE);
  317. cdev->mode_info.crtc = cirrus_crtc;
  318. drm_crtc_helper_add(&cirrus_crtc->base, &cirrus_helper_funcs);
  319. }
  320. static void cirrus_encoder_mode_set(struct drm_encoder *encoder,
  321. struct drm_display_mode *mode,
  322. struct drm_display_mode *adjusted_mode)
  323. {
  324. }
  325. static void cirrus_encoder_dpms(struct drm_encoder *encoder, int state)
  326. {
  327. return;
  328. }
  329. static void cirrus_encoder_prepare(struct drm_encoder *encoder)
  330. {
  331. }
  332. static void cirrus_encoder_commit(struct drm_encoder *encoder)
  333. {
  334. }
  335. static void cirrus_encoder_destroy(struct drm_encoder *encoder)
  336. {
  337. struct cirrus_encoder *cirrus_encoder = to_cirrus_encoder(encoder);
  338. drm_encoder_cleanup(encoder);
  339. kfree(cirrus_encoder);
  340. }
  341. static const struct drm_encoder_helper_funcs cirrus_encoder_helper_funcs = {
  342. .dpms = cirrus_encoder_dpms,
  343. .mode_set = cirrus_encoder_mode_set,
  344. .prepare = cirrus_encoder_prepare,
  345. .commit = cirrus_encoder_commit,
  346. };
  347. static const struct drm_encoder_funcs cirrus_encoder_encoder_funcs = {
  348. .destroy = cirrus_encoder_destroy,
  349. };
  350. static struct drm_encoder *cirrus_encoder_init(struct drm_device *dev)
  351. {
  352. struct drm_encoder *encoder;
  353. struct cirrus_encoder *cirrus_encoder;
  354. cirrus_encoder = kzalloc(sizeof(struct cirrus_encoder), GFP_KERNEL);
  355. if (!cirrus_encoder)
  356. return NULL;
  357. encoder = &cirrus_encoder->base;
  358. encoder->possible_crtcs = 0x1;
  359. drm_encoder_init(dev, encoder, &cirrus_encoder_encoder_funcs,
  360. DRM_MODE_ENCODER_DAC, NULL);
  361. drm_encoder_helper_add(encoder, &cirrus_encoder_helper_funcs);
  362. return encoder;
  363. }
  364. static int cirrus_vga_get_modes(struct drm_connector *connector)
  365. {
  366. int count;
  367. /* Just add a static list of modes */
  368. if (cirrus_bpp <= 24) {
  369. count = drm_add_modes_noedid(connector, 1280, 1024);
  370. drm_set_preferred_mode(connector, 1024, 768);
  371. } else {
  372. count = drm_add_modes_noedid(connector, 800, 600);
  373. drm_set_preferred_mode(connector, 800, 600);
  374. }
  375. return count;
  376. }
  377. static struct drm_encoder *cirrus_connector_best_encoder(struct drm_connector
  378. *connector)
  379. {
  380. int enc_id = connector->encoder_ids[0];
  381. /* pick the encoder ids */
  382. if (enc_id)
  383. return drm_encoder_find(connector->dev, NULL, enc_id);
  384. return NULL;
  385. }
  386. static void cirrus_connector_destroy(struct drm_connector *connector)
  387. {
  388. drm_connector_cleanup(connector);
  389. kfree(connector);
  390. }
  391. static const struct drm_connector_helper_funcs cirrus_vga_connector_helper_funcs = {
  392. .get_modes = cirrus_vga_get_modes,
  393. .best_encoder = cirrus_connector_best_encoder,
  394. };
  395. static const struct drm_connector_funcs cirrus_vga_connector_funcs = {
  396. .dpms = drm_helper_connector_dpms,
  397. .fill_modes = drm_helper_probe_single_connector_modes,
  398. .destroy = cirrus_connector_destroy,
  399. };
  400. static struct drm_connector *cirrus_vga_init(struct drm_device *dev)
  401. {
  402. struct drm_connector *connector;
  403. struct cirrus_connector *cirrus_connector;
  404. cirrus_connector = kzalloc(sizeof(struct cirrus_connector), GFP_KERNEL);
  405. if (!cirrus_connector)
  406. return NULL;
  407. connector = &cirrus_connector->base;
  408. drm_connector_init(dev, connector,
  409. &cirrus_vga_connector_funcs, DRM_MODE_CONNECTOR_VGA);
  410. drm_connector_helper_add(connector, &cirrus_vga_connector_helper_funcs);
  411. drm_connector_register(connector);
  412. return connector;
  413. }
  414. int cirrus_modeset_init(struct cirrus_device *cdev)
  415. {
  416. struct drm_encoder *encoder;
  417. struct drm_connector *connector;
  418. int ret;
  419. drm_mode_config_init(cdev->dev);
  420. cdev->mode_info.mode_config_initialized = true;
  421. cdev->dev->mode_config.max_width = CIRRUS_MAX_FB_WIDTH;
  422. cdev->dev->mode_config.max_height = CIRRUS_MAX_FB_HEIGHT;
  423. cdev->dev->mode_config.fb_base = cdev->mc.vram_base;
  424. cdev->dev->mode_config.preferred_depth = cirrus_bpp;
  425. /* don't prefer a shadow on virt GPU */
  426. cdev->dev->mode_config.prefer_shadow = 0;
  427. cirrus_crtc_init(cdev->dev);
  428. encoder = cirrus_encoder_init(cdev->dev);
  429. if (!encoder) {
  430. DRM_ERROR("cirrus_encoder_init failed\n");
  431. return -1;
  432. }
  433. connector = cirrus_vga_init(cdev->dev);
  434. if (!connector) {
  435. DRM_ERROR("cirrus_vga_init failed\n");
  436. return -1;
  437. }
  438. drm_connector_attach_encoder(connector, encoder);
  439. ret = cirrus_fbdev_init(cdev);
  440. if (ret) {
  441. DRM_ERROR("cirrus_fbdev_init failed\n");
  442. return ret;
  443. }
  444. return 0;
  445. }
  446. void cirrus_modeset_fini(struct cirrus_device *cdev)
  447. {
  448. cirrus_fbdev_fini(cdev);
  449. if (cdev->mode_info.mode_config_initialized) {
  450. drm_mode_config_cleanup(cdev->dev);
  451. cdev->mode_info.mode_config_initialized = false;
  452. }
  453. }