ast_main.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  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. if (dev->pdev->device == PCI_CHIP_AST1180) {
  62. ast->chip = AST1100;
  63. DRM_INFO("AST 1180 detected\n");
  64. } else {
  65. if (dev->pdev->revision >= 0x20) {
  66. ast->chip = AST2300;
  67. DRM_INFO("AST 2300 detected\n");
  68. } else if (dev->pdev->revision >= 0x10) {
  69. uint32_t data;
  70. ast_write32(ast, 0xf004, 0x1e6e0000);
  71. ast_write32(ast, 0xf000, 0x1);
  72. data = ast_read32(ast, 0x1207c);
  73. switch (data & 0x0300) {
  74. case 0x0200:
  75. ast->chip = AST1100;
  76. DRM_INFO("AST 1100 detected\n");
  77. break;
  78. case 0x0100:
  79. ast->chip = AST2200;
  80. DRM_INFO("AST 2200 detected\n");
  81. break;
  82. case 0x0000:
  83. ast->chip = AST2150;
  84. DRM_INFO("AST 2150 detected\n");
  85. break;
  86. default:
  87. ast->chip = AST2100;
  88. DRM_INFO("AST 2100 detected\n");
  89. break;
  90. }
  91. ast->vga2_clone = false;
  92. } else {
  93. ast->chip = 2000;
  94. DRM_INFO("AST 2000 detected\n");
  95. }
  96. }
  97. return 0;
  98. }
  99. static int ast_get_dram_info(struct drm_device *dev)
  100. {
  101. struct ast_private *ast = dev->dev_private;
  102. uint32_t data, data2;
  103. uint32_t denum, num, div, ref_pll;
  104. ast_write32(ast, 0xf004, 0x1e6e0000);
  105. ast_write32(ast, 0xf000, 0x1);
  106. ast_write32(ast, 0x10000, 0xfc600309);
  107. do {
  108. ;
  109. } while (ast_read32(ast, 0x10000) != 0x01);
  110. data = ast_read32(ast, 0x10004);
  111. if (data & 0x400)
  112. ast->dram_bus_width = 16;
  113. else
  114. ast->dram_bus_width = 32;
  115. if (ast->chip == AST2300) {
  116. switch (data & 0x03) {
  117. case 0:
  118. ast->dram_type = AST_DRAM_512Mx16;
  119. break;
  120. default:
  121. case 1:
  122. ast->dram_type = AST_DRAM_1Gx16;
  123. break;
  124. case 2:
  125. ast->dram_type = AST_DRAM_2Gx16;
  126. break;
  127. case 3:
  128. ast->dram_type = AST_DRAM_4Gx16;
  129. break;
  130. }
  131. } else {
  132. switch (data & 0x0c) {
  133. case 0:
  134. case 4:
  135. ast->dram_type = AST_DRAM_512Mx16;
  136. break;
  137. case 8:
  138. if (data & 0x40)
  139. ast->dram_type = AST_DRAM_1Gx16;
  140. else
  141. ast->dram_type = AST_DRAM_512Mx32;
  142. break;
  143. case 0xc:
  144. ast->dram_type = AST_DRAM_1Gx32;
  145. break;
  146. }
  147. }
  148. data = ast_read32(ast, 0x10120);
  149. data2 = ast_read32(ast, 0x10170);
  150. if (data2 & 0x2000)
  151. ref_pll = 14318;
  152. else
  153. ref_pll = 12000;
  154. denum = data & 0x1f;
  155. num = (data & 0x3fe0) >> 5;
  156. data = (data & 0xc000) >> 14;
  157. switch (data) {
  158. case 3:
  159. div = 0x4;
  160. break;
  161. case 2:
  162. case 1:
  163. div = 0x2;
  164. break;
  165. default:
  166. div = 0x1;
  167. break;
  168. }
  169. ast->mclk = ref_pll * (num + 2) / (denum + 2) * (div * 1000);
  170. return 0;
  171. }
  172. uint32_t ast_get_max_dclk(struct drm_device *dev, int bpp)
  173. {
  174. struct ast_private *ast = dev->dev_private;
  175. uint32_t dclk, jreg;
  176. uint32_t dram_bus_width, mclk, dram_bandwidth, actual_dram_bandwidth, dram_efficency = 500;
  177. dram_bus_width = ast->dram_bus_width;
  178. mclk = ast->mclk;
  179. if (ast->chip == AST2100 ||
  180. ast->chip == AST1100 ||
  181. ast->chip == AST2200 ||
  182. ast->chip == AST2150 ||
  183. ast->dram_bus_width == 16)
  184. dram_efficency = 600;
  185. else if (ast->chip == AST2300)
  186. dram_efficency = 400;
  187. dram_bandwidth = mclk * dram_bus_width * 2 / 8;
  188. actual_dram_bandwidth = dram_bandwidth * dram_efficency / 1000;
  189. if (ast->chip == AST1180)
  190. dclk = actual_dram_bandwidth / ((bpp + 1) / 8);
  191. else {
  192. jreg = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xd0, 0xff);
  193. if ((jreg & 0x08) && (ast->chip == AST2000))
  194. dclk = actual_dram_bandwidth / ((bpp + 1 + 16) / 8);
  195. else if ((jreg & 0x08) && (bpp == 8))
  196. dclk = actual_dram_bandwidth / ((bpp + 1 + 24) / 8);
  197. else
  198. dclk = actual_dram_bandwidth / ((bpp + 1) / 8);
  199. }
  200. if (ast->chip == AST2100 ||
  201. ast->chip == AST2200 ||
  202. ast->chip == AST2300 ||
  203. ast->chip == AST1180) {
  204. if (dclk > 200)
  205. dclk = 200;
  206. } else {
  207. if (dclk > 165)
  208. dclk = 165;
  209. }
  210. return dclk;
  211. }
  212. static void ast_user_framebuffer_destroy(struct drm_framebuffer *fb)
  213. {
  214. struct ast_framebuffer *ast_fb = to_ast_framebuffer(fb);
  215. if (ast_fb->obj)
  216. drm_gem_object_unreference_unlocked(ast_fb->obj);
  217. drm_framebuffer_cleanup(fb);
  218. kfree(fb);
  219. }
  220. static int ast_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  221. struct drm_file *file,
  222. unsigned int *handle)
  223. {
  224. return -EINVAL;
  225. }
  226. static const struct drm_framebuffer_funcs ast_fb_funcs = {
  227. .destroy = ast_user_framebuffer_destroy,
  228. .create_handle = ast_user_framebuffer_create_handle,
  229. };
  230. int ast_framebuffer_init(struct drm_device *dev,
  231. struct ast_framebuffer *ast_fb,
  232. struct drm_mode_fb_cmd2 *mode_cmd,
  233. struct drm_gem_object *obj)
  234. {
  235. int ret;
  236. ret = drm_framebuffer_init(dev, &ast_fb->base, &ast_fb_funcs);
  237. if (ret) {
  238. DRM_ERROR("framebuffer init failed %d\n", ret);
  239. return ret;
  240. }
  241. drm_helper_mode_fill_fb_struct(&ast_fb->base, mode_cmd);
  242. ast_fb->obj = obj;
  243. return 0;
  244. }
  245. static struct drm_framebuffer *
  246. ast_user_framebuffer_create(struct drm_device *dev,
  247. struct drm_file *filp,
  248. struct drm_mode_fb_cmd2 *mode_cmd)
  249. {
  250. struct drm_gem_object *obj;
  251. struct ast_framebuffer *ast_fb;
  252. int ret;
  253. obj = drm_gem_object_lookup(dev, filp, mode_cmd->handles[0]);
  254. if (obj == NULL)
  255. return ERR_PTR(-ENOENT);
  256. ast_fb = kzalloc(sizeof(*ast_fb), GFP_KERNEL);
  257. if (!ast_fb) {
  258. drm_gem_object_unreference_unlocked(obj);
  259. return ERR_PTR(-ENOMEM);
  260. }
  261. ret = ast_framebuffer_init(dev, ast_fb, mode_cmd, obj);
  262. if (ret) {
  263. drm_gem_object_unreference_unlocked(obj);
  264. kfree(ast_fb);
  265. return ERR_PTR(ret);
  266. }
  267. return &ast_fb->base;
  268. }
  269. static const struct drm_mode_config_funcs ast_mode_funcs = {
  270. .fb_create = ast_user_framebuffer_create,
  271. };
  272. static u32 ast_get_vram_info(struct drm_device *dev)
  273. {
  274. struct ast_private *ast = dev->dev_private;
  275. u8 jreg;
  276. ast_open_key(ast);
  277. jreg = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xaa, 0xff);
  278. switch (jreg & 3) {
  279. case 0: return AST_VIDMEM_SIZE_8M;
  280. case 1: return AST_VIDMEM_SIZE_16M;
  281. case 2: return AST_VIDMEM_SIZE_32M;
  282. case 3: return AST_VIDMEM_SIZE_64M;
  283. }
  284. return AST_VIDMEM_DEFAULT_SIZE;
  285. }
  286. int ast_driver_load(struct drm_device *dev, unsigned long flags)
  287. {
  288. struct ast_private *ast;
  289. int ret = 0;
  290. ast = kzalloc(sizeof(struct ast_private), GFP_KERNEL);
  291. if (!ast)
  292. return -ENOMEM;
  293. dev->dev_private = ast;
  294. ast->dev = dev;
  295. ast->regs = pci_iomap(dev->pdev, 1, 0);
  296. if (!ast->regs) {
  297. ret = -EIO;
  298. goto out_free;
  299. }
  300. ast->ioregs = pci_iomap(dev->pdev, 2, 0);
  301. if (!ast->ioregs) {
  302. ret = -EIO;
  303. goto out_free;
  304. }
  305. ast_detect_chip(dev);
  306. if (ast->chip != AST1180) {
  307. ast_get_dram_info(dev);
  308. ast->vram_size = ast_get_vram_info(dev);
  309. DRM_INFO("dram %d %d %d %08x\n", ast->mclk, ast->dram_type, ast->dram_bus_width, ast->vram_size);
  310. }
  311. ret = ast_mm_init(ast);
  312. if (ret)
  313. goto out_free;
  314. drm_mode_config_init(dev);
  315. dev->mode_config.funcs = (void *)&ast_mode_funcs;
  316. dev->mode_config.min_width = 0;
  317. dev->mode_config.min_height = 0;
  318. dev->mode_config.preferred_depth = 24;
  319. dev->mode_config.prefer_shadow = 1;
  320. if (ast->chip == AST2100 ||
  321. ast->chip == AST2200 ||
  322. ast->chip == AST2300 ||
  323. ast->chip == AST1180) {
  324. dev->mode_config.max_width = 1920;
  325. dev->mode_config.max_height = 2048;
  326. } else {
  327. dev->mode_config.max_width = 1600;
  328. dev->mode_config.max_height = 1200;
  329. }
  330. ret = ast_mode_init(dev);
  331. if (ret)
  332. goto out_free;
  333. ret = ast_fbdev_init(dev);
  334. if (ret)
  335. goto out_free;
  336. return 0;
  337. out_free:
  338. kfree(ast);
  339. dev->dev_private = NULL;
  340. return ret;
  341. }
  342. int ast_driver_unload(struct drm_device *dev)
  343. {
  344. struct ast_private *ast = dev->dev_private;
  345. ast_mode_fini(dev);
  346. ast_fbdev_fini(dev);
  347. drm_mode_config_cleanup(dev);
  348. ast_mm_fini(ast);
  349. pci_iounmap(dev->pdev, ast->ioregs);
  350. pci_iounmap(dev->pdev, ast->regs);
  351. kfree(ast);
  352. return 0;
  353. }
  354. int ast_gem_create(struct drm_device *dev,
  355. u32 size, bool iskernel,
  356. struct drm_gem_object **obj)
  357. {
  358. struct ast_bo *astbo;
  359. int ret;
  360. *obj = NULL;
  361. size = roundup(size, PAGE_SIZE);
  362. if (size == 0)
  363. return -EINVAL;
  364. ret = ast_bo_create(dev, size, 0, 0, &astbo);
  365. if (ret) {
  366. if (ret != -ERESTARTSYS)
  367. DRM_ERROR("failed to allocate GEM object\n");
  368. return ret;
  369. }
  370. *obj = &astbo->gem;
  371. return 0;
  372. }
  373. int ast_dumb_create(struct drm_file *file,
  374. struct drm_device *dev,
  375. struct drm_mode_create_dumb *args)
  376. {
  377. int ret;
  378. struct drm_gem_object *gobj;
  379. u32 handle;
  380. args->pitch = args->width * ((args->bpp + 7) / 8);
  381. args->size = args->pitch * args->height;
  382. ret = ast_gem_create(dev, args->size, false,
  383. &gobj);
  384. if (ret)
  385. return ret;
  386. ret = drm_gem_handle_create(file, gobj, &handle);
  387. drm_gem_object_unreference_unlocked(gobj);
  388. if (ret)
  389. return ret;
  390. args->handle = handle;
  391. return 0;
  392. }
  393. int ast_dumb_destroy(struct drm_file *file,
  394. struct drm_device *dev,
  395. uint32_t handle)
  396. {
  397. return drm_gem_handle_delete(file, handle);
  398. }
  399. int ast_gem_init_object(struct drm_gem_object *obj)
  400. {
  401. BUG();
  402. return 0;
  403. }
  404. void ast_bo_unref(struct ast_bo **bo)
  405. {
  406. struct ttm_buffer_object *tbo;
  407. if ((*bo) == NULL)
  408. return;
  409. tbo = &((*bo)->bo);
  410. ttm_bo_unref(&tbo);
  411. if (tbo == NULL)
  412. *bo = NULL;
  413. }
  414. void ast_gem_free_object(struct drm_gem_object *obj)
  415. {
  416. struct ast_bo *ast_bo = gem_to_ast_bo(obj);
  417. if (!ast_bo)
  418. return;
  419. ast_bo_unref(&ast_bo);
  420. }
  421. static inline u64 ast_bo_mmap_offset(struct ast_bo *bo)
  422. {
  423. return bo->bo.addr_space_offset;
  424. }
  425. int
  426. ast_dumb_mmap_offset(struct drm_file *file,
  427. struct drm_device *dev,
  428. uint32_t handle,
  429. uint64_t *offset)
  430. {
  431. struct drm_gem_object *obj;
  432. int ret;
  433. struct ast_bo *bo;
  434. mutex_lock(&dev->struct_mutex);
  435. obj = drm_gem_object_lookup(dev, file, handle);
  436. if (obj == NULL) {
  437. ret = -ENOENT;
  438. goto out_unlock;
  439. }
  440. bo = gem_to_ast_bo(obj);
  441. *offset = ast_bo_mmap_offset(bo);
  442. drm_gem_object_unreference(obj);
  443. ret = 0;
  444. out_unlock:
  445. mutex_unlock(&dev->struct_mutex);
  446. return ret;
  447. }