ast_main.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. /*
  2. * Copyright 2012 Red Hat Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the
  6. * "Software"), to deal in the Software without restriction, including
  7. * without limitation the rights to use, copy, modify, merge, publish,
  8. * distribute, sub license, and/or sell copies of the Software, and to
  9. * permit persons to whom the Software is furnished to do so, subject to
  10. * the following conditions:
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  15. * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
  16. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  17. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  18. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  19. *
  20. * The above copyright notice and this permission notice (including the
  21. * next paragraph) shall be included in all copies or substantial portions
  22. * of the Software.
  23. *
  24. */
  25. /*
  26. * Authors: Dave Airlie <airlied@redhat.com>
  27. */
  28. #include <drm/drmP.h>
  29. #include "ast_drv.h"
  30. #include <drm/drm_fb_helper.h>
  31. #include <drm/drm_crtc_helper.h>
  32. #include "ast_dram_tables.h"
  33. void ast_set_index_reg_mask(struct ast_private *ast,
  34. uint32_t base, uint8_t index,
  35. uint8_t mask, uint8_t val)
  36. {
  37. u8 tmp;
  38. ast_io_write8(ast, base, index);
  39. tmp = (ast_io_read8(ast, base + 1) & mask) | val;
  40. ast_set_index_reg(ast, base, index, tmp);
  41. }
  42. uint8_t ast_get_index_reg(struct ast_private *ast,
  43. uint32_t base, uint8_t index)
  44. {
  45. uint8_t ret;
  46. ast_io_write8(ast, base, index);
  47. ret = ast_io_read8(ast, base + 1);
  48. return ret;
  49. }
  50. uint8_t ast_get_index_reg_mask(struct ast_private *ast,
  51. uint32_t base, uint8_t index, uint8_t mask)
  52. {
  53. uint8_t ret;
  54. ast_io_write8(ast, base, index);
  55. ret = ast_io_read8(ast, base + 1) & mask;
  56. return ret;
  57. }
  58. static int ast_detect_chip(struct drm_device *dev)
  59. {
  60. struct ast_private *ast = dev->dev_private;
  61. uint32_t data, jreg;
  62. if (dev->pdev->device == PCI_CHIP_AST1180) {
  63. ast->chip = AST1100;
  64. DRM_INFO("AST 1180 detected\n");
  65. } else {
  66. if (dev->pdev->revision >= 0x30) {
  67. ast->chip = AST2400;
  68. DRM_INFO("AST 2400 detected\n");
  69. } else if (dev->pdev->revision >= 0x20) {
  70. ast->chip = AST2300;
  71. DRM_INFO("AST 2300 detected\n");
  72. } else if (dev->pdev->revision >= 0x10) {
  73. uint32_t data;
  74. ast_write32(ast, 0xf004, 0x1e6e0000);
  75. ast_write32(ast, 0xf000, 0x1);
  76. data = ast_read32(ast, 0x1207c);
  77. switch (data & 0x0300) {
  78. case 0x0200:
  79. ast->chip = AST1100;
  80. DRM_INFO("AST 1100 detected\n");
  81. break;
  82. case 0x0100:
  83. ast->chip = AST2200;
  84. DRM_INFO("AST 2200 detected\n");
  85. break;
  86. case 0x0000:
  87. ast->chip = AST2150;
  88. DRM_INFO("AST 2150 detected\n");
  89. break;
  90. default:
  91. ast->chip = AST2100;
  92. DRM_INFO("AST 2100 detected\n");
  93. break;
  94. }
  95. ast->vga2_clone = false;
  96. } else {
  97. ast->chip = 2000;
  98. DRM_INFO("AST 2000 detected\n");
  99. }
  100. }
  101. switch (ast->chip) {
  102. case AST1180:
  103. ast->support_wide_screen = true;
  104. break;
  105. case AST2000:
  106. ast->support_wide_screen = false;
  107. break;
  108. default:
  109. jreg = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xd0, 0xff);
  110. if (!(jreg & 0x80))
  111. ast->support_wide_screen = true;
  112. else if (jreg & 0x01)
  113. ast->support_wide_screen = true;
  114. else {
  115. ast->support_wide_screen = false;
  116. ast_write32(ast, 0xf004, 0x1e6e0000);
  117. ast_write32(ast, 0xf000, 0x1);
  118. data = ast_read32(ast, 0x1207c);
  119. data &= 0x300;
  120. if (ast->chip == AST2300 && data == 0x0) /* ast1300 */
  121. ast->support_wide_screen = true;
  122. if (ast->chip == AST2400 && data == 0x100) /* ast1400 */
  123. ast->support_wide_screen = true;
  124. }
  125. break;
  126. }
  127. ast->tx_chip_type = AST_TX_NONE;
  128. jreg = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xa3, 0xff);
  129. if (jreg & 0x80)
  130. ast->tx_chip_type = AST_TX_SIL164;
  131. if ((ast->chip == AST2300) || (ast->chip == AST2400)) {
  132. jreg = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xd1, 0xff);
  133. switch (jreg) {
  134. case 0x04:
  135. ast->tx_chip_type = AST_TX_SIL164;
  136. break;
  137. case 0x08:
  138. ast->dp501_fw_addr = kzalloc(32*1024, GFP_KERNEL);
  139. if (ast->dp501_fw_addr) {
  140. /* backup firmware */
  141. if (ast_backup_fw(dev, ast->dp501_fw_addr, 32*1024)) {
  142. kfree(ast->dp501_fw_addr);
  143. ast->dp501_fw_addr = NULL;
  144. }
  145. }
  146. /* fallthrough */
  147. case 0x0c:
  148. ast->tx_chip_type = AST_TX_DP501;
  149. }
  150. }
  151. return 0;
  152. }
  153. static int ast_get_dram_info(struct drm_device *dev)
  154. {
  155. struct ast_private *ast = dev->dev_private;
  156. uint32_t data, data2;
  157. uint32_t denum, num, div, ref_pll;
  158. ast_write32(ast, 0xf004, 0x1e6e0000);
  159. ast_write32(ast, 0xf000, 0x1);
  160. ast_write32(ast, 0x10000, 0xfc600309);
  161. do {
  162. ;
  163. } while (ast_read32(ast, 0x10000) != 0x01);
  164. data = ast_read32(ast, 0x10004);
  165. if (data & 0x400)
  166. ast->dram_bus_width = 16;
  167. else
  168. ast->dram_bus_width = 32;
  169. if (ast->chip == AST2300 || ast->chip == AST2400) {
  170. switch (data & 0x03) {
  171. case 0:
  172. ast->dram_type = AST_DRAM_512Mx16;
  173. break;
  174. default:
  175. case 1:
  176. ast->dram_type = AST_DRAM_1Gx16;
  177. break;
  178. case 2:
  179. ast->dram_type = AST_DRAM_2Gx16;
  180. break;
  181. case 3:
  182. ast->dram_type = AST_DRAM_4Gx16;
  183. break;
  184. }
  185. } else {
  186. switch (data & 0x0c) {
  187. case 0:
  188. case 4:
  189. ast->dram_type = AST_DRAM_512Mx16;
  190. break;
  191. case 8:
  192. if (data & 0x40)
  193. ast->dram_type = AST_DRAM_1Gx16;
  194. else
  195. ast->dram_type = AST_DRAM_512Mx32;
  196. break;
  197. case 0xc:
  198. ast->dram_type = AST_DRAM_1Gx32;
  199. break;
  200. }
  201. }
  202. data = ast_read32(ast, 0x10120);
  203. data2 = ast_read32(ast, 0x10170);
  204. if (data2 & 0x2000)
  205. ref_pll = 14318;
  206. else
  207. ref_pll = 12000;
  208. denum = data & 0x1f;
  209. num = (data & 0x3fe0) >> 5;
  210. data = (data & 0xc000) >> 14;
  211. switch (data) {
  212. case 3:
  213. div = 0x4;
  214. break;
  215. case 2:
  216. case 1:
  217. div = 0x2;
  218. break;
  219. default:
  220. div = 0x1;
  221. break;
  222. }
  223. ast->mclk = ref_pll * (num + 2) / (denum + 2) * (div * 1000);
  224. return 0;
  225. }
  226. static void ast_user_framebuffer_destroy(struct drm_framebuffer *fb)
  227. {
  228. struct ast_framebuffer *ast_fb = to_ast_framebuffer(fb);
  229. if (ast_fb->obj)
  230. drm_gem_object_unreference_unlocked(ast_fb->obj);
  231. drm_framebuffer_cleanup(fb);
  232. kfree(fb);
  233. }
  234. static const struct drm_framebuffer_funcs ast_fb_funcs = {
  235. .destroy = ast_user_framebuffer_destroy,
  236. };
  237. int ast_framebuffer_init(struct drm_device *dev,
  238. struct ast_framebuffer *ast_fb,
  239. struct drm_mode_fb_cmd2 *mode_cmd,
  240. struct drm_gem_object *obj)
  241. {
  242. int ret;
  243. drm_helper_mode_fill_fb_struct(&ast_fb->base, mode_cmd);
  244. ast_fb->obj = obj;
  245. ret = drm_framebuffer_init(dev, &ast_fb->base, &ast_fb_funcs);
  246. if (ret) {
  247. DRM_ERROR("framebuffer init failed %d\n", ret);
  248. return ret;
  249. }
  250. return 0;
  251. }
  252. static struct drm_framebuffer *
  253. ast_user_framebuffer_create(struct drm_device *dev,
  254. struct drm_file *filp,
  255. struct drm_mode_fb_cmd2 *mode_cmd)
  256. {
  257. struct drm_gem_object *obj;
  258. struct ast_framebuffer *ast_fb;
  259. int ret;
  260. obj = drm_gem_object_lookup(dev, filp, mode_cmd->handles[0]);
  261. if (obj == NULL)
  262. return ERR_PTR(-ENOENT);
  263. ast_fb = kzalloc(sizeof(*ast_fb), GFP_KERNEL);
  264. if (!ast_fb) {
  265. drm_gem_object_unreference_unlocked(obj);
  266. return ERR_PTR(-ENOMEM);
  267. }
  268. ret = ast_framebuffer_init(dev, ast_fb, mode_cmd, obj);
  269. if (ret) {
  270. drm_gem_object_unreference_unlocked(obj);
  271. kfree(ast_fb);
  272. return ERR_PTR(ret);
  273. }
  274. return &ast_fb->base;
  275. }
  276. static const struct drm_mode_config_funcs ast_mode_funcs = {
  277. .fb_create = ast_user_framebuffer_create,
  278. };
  279. static u32 ast_get_vram_info(struct drm_device *dev)
  280. {
  281. struct ast_private *ast = dev->dev_private;
  282. u8 jreg;
  283. u32 vram_size;
  284. ast_open_key(ast);
  285. vram_size = AST_VIDMEM_DEFAULT_SIZE;
  286. jreg = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xaa, 0xff);
  287. switch (jreg & 3) {
  288. case 0: vram_size = AST_VIDMEM_SIZE_8M; break;
  289. case 1: vram_size = AST_VIDMEM_SIZE_16M; break;
  290. case 2: vram_size = AST_VIDMEM_SIZE_32M; break;
  291. case 3: vram_size = AST_VIDMEM_SIZE_64M; break;
  292. }
  293. jreg = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x99, 0xff);
  294. switch (jreg & 0x03) {
  295. case 1:
  296. vram_size -= 0x100000;
  297. break;
  298. case 2:
  299. vram_size -= 0x200000;
  300. break;
  301. case 3:
  302. vram_size -= 0x400000;
  303. break;
  304. }
  305. return vram_size;
  306. }
  307. int ast_driver_load(struct drm_device *dev, unsigned long flags)
  308. {
  309. struct ast_private *ast;
  310. int ret = 0;
  311. ast = kzalloc(sizeof(struct ast_private), GFP_KERNEL);
  312. if (!ast)
  313. return -ENOMEM;
  314. dev->dev_private = ast;
  315. ast->dev = dev;
  316. ast->regs = pci_iomap(dev->pdev, 1, 0);
  317. if (!ast->regs) {
  318. ret = -EIO;
  319. goto out_free;
  320. }
  321. ast->ioregs = pci_iomap(dev->pdev, 2, 0);
  322. if (!ast->ioregs) {
  323. ret = -EIO;
  324. goto out_free;
  325. }
  326. ast_detect_chip(dev);
  327. if (ast->chip != AST1180) {
  328. ast_get_dram_info(dev);
  329. ast->vram_size = ast_get_vram_info(dev);
  330. DRM_INFO("dram %d %d %d %08x\n", ast->mclk, ast->dram_type, ast->dram_bus_width, ast->vram_size);
  331. }
  332. ret = ast_mm_init(ast);
  333. if (ret)
  334. goto out_free;
  335. drm_mode_config_init(dev);
  336. dev->mode_config.funcs = (void *)&ast_mode_funcs;
  337. dev->mode_config.min_width = 0;
  338. dev->mode_config.min_height = 0;
  339. dev->mode_config.preferred_depth = 24;
  340. dev->mode_config.prefer_shadow = 1;
  341. if (ast->chip == AST2100 ||
  342. ast->chip == AST2200 ||
  343. ast->chip == AST2300 ||
  344. ast->chip == AST2400 ||
  345. ast->chip == AST1180) {
  346. dev->mode_config.max_width = 1920;
  347. dev->mode_config.max_height = 2048;
  348. } else {
  349. dev->mode_config.max_width = 1600;
  350. dev->mode_config.max_height = 1200;
  351. }
  352. ret = ast_mode_init(dev);
  353. if (ret)
  354. goto out_free;
  355. ret = ast_fbdev_init(dev);
  356. if (ret)
  357. goto out_free;
  358. return 0;
  359. out_free:
  360. kfree(ast);
  361. dev->dev_private = NULL;
  362. return ret;
  363. }
  364. int ast_driver_unload(struct drm_device *dev)
  365. {
  366. struct ast_private *ast = dev->dev_private;
  367. kfree(ast->dp501_fw_addr);
  368. ast_mode_fini(dev);
  369. ast_fbdev_fini(dev);
  370. drm_mode_config_cleanup(dev);
  371. ast_mm_fini(ast);
  372. pci_iounmap(dev->pdev, ast->ioregs);
  373. pci_iounmap(dev->pdev, ast->regs);
  374. kfree(ast);
  375. return 0;
  376. }
  377. int ast_gem_create(struct drm_device *dev,
  378. u32 size, bool iskernel,
  379. struct drm_gem_object **obj)
  380. {
  381. struct ast_bo *astbo;
  382. int ret;
  383. *obj = NULL;
  384. size = roundup(size, PAGE_SIZE);
  385. if (size == 0)
  386. return -EINVAL;
  387. ret = ast_bo_create(dev, size, 0, 0, &astbo);
  388. if (ret) {
  389. if (ret != -ERESTARTSYS)
  390. DRM_ERROR("failed to allocate GEM object\n");
  391. return ret;
  392. }
  393. *obj = &astbo->gem;
  394. return 0;
  395. }
  396. int ast_dumb_create(struct drm_file *file,
  397. struct drm_device *dev,
  398. struct drm_mode_create_dumb *args)
  399. {
  400. int ret;
  401. struct drm_gem_object *gobj;
  402. u32 handle;
  403. args->pitch = args->width * ((args->bpp + 7) / 8);
  404. args->size = args->pitch * args->height;
  405. ret = ast_gem_create(dev, args->size, false,
  406. &gobj);
  407. if (ret)
  408. return ret;
  409. ret = drm_gem_handle_create(file, gobj, &handle);
  410. drm_gem_object_unreference_unlocked(gobj);
  411. if (ret)
  412. return ret;
  413. args->handle = handle;
  414. return 0;
  415. }
  416. static void ast_bo_unref(struct ast_bo **bo)
  417. {
  418. struct ttm_buffer_object *tbo;
  419. if ((*bo) == NULL)
  420. return;
  421. tbo = &((*bo)->bo);
  422. ttm_bo_unref(&tbo);
  423. *bo = NULL;
  424. }
  425. void ast_gem_free_object(struct drm_gem_object *obj)
  426. {
  427. struct ast_bo *ast_bo = gem_to_ast_bo(obj);
  428. ast_bo_unref(&ast_bo);
  429. }
  430. static inline u64 ast_bo_mmap_offset(struct ast_bo *bo)
  431. {
  432. return drm_vma_node_offset_addr(&bo->bo.vma_node);
  433. }
  434. int
  435. ast_dumb_mmap_offset(struct drm_file *file,
  436. struct drm_device *dev,
  437. uint32_t handle,
  438. uint64_t *offset)
  439. {
  440. struct drm_gem_object *obj;
  441. int ret;
  442. struct ast_bo *bo;
  443. mutex_lock(&dev->struct_mutex);
  444. obj = drm_gem_object_lookup(dev, file, handle);
  445. if (obj == NULL) {
  446. ret = -ENOENT;
  447. goto out_unlock;
  448. }
  449. bo = gem_to_ast_bo(obj);
  450. *offset = ast_bo_mmap_offset(bo);
  451. drm_gem_object_unreference(obj);
  452. ret = 0;
  453. out_unlock:
  454. mutex_unlock(&dev->struct_mutex);
  455. return ret;
  456. }