amdgpu_atombios.c 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819
  1. /*
  2. * Copyright 2007-8 Advanced Micro Devices, Inc.
  3. * Copyright 2008 Red Hat Inc.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the "Software"),
  7. * to deal in the Software without restriction, including without limitation
  8. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. * and/or sell copies of the Software, and to permit persons to whom the
  10. * Software is furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  19. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  20. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. * OTHER DEALINGS IN THE SOFTWARE.
  22. *
  23. * Authors: Dave Airlie
  24. * Alex Deucher
  25. */
  26. #include <drm/drmP.h>
  27. #include <drm/amdgpu_drm.h>
  28. #include "amdgpu.h"
  29. #include "amdgpu_atombios.h"
  30. #include "amdgpu_i2c.h"
  31. #include "atom.h"
  32. #include "atom-bits.h"
  33. #include "atombios_encoders.h"
  34. #include "bif/bif_4_1_d.h"
  35. static void amdgpu_atombios_lookup_i2c_gpio_quirks(struct amdgpu_device *adev,
  36. ATOM_GPIO_I2C_ASSIGMENT *gpio,
  37. u8 index)
  38. {
  39. }
  40. static struct amdgpu_i2c_bus_rec amdgpu_atombios_get_bus_rec_for_i2c_gpio(ATOM_GPIO_I2C_ASSIGMENT *gpio)
  41. {
  42. struct amdgpu_i2c_bus_rec i2c;
  43. memset(&i2c, 0, sizeof(struct amdgpu_i2c_bus_rec));
  44. i2c.mask_clk_reg = le16_to_cpu(gpio->usClkMaskRegisterIndex);
  45. i2c.mask_data_reg = le16_to_cpu(gpio->usDataMaskRegisterIndex);
  46. i2c.en_clk_reg = le16_to_cpu(gpio->usClkEnRegisterIndex);
  47. i2c.en_data_reg = le16_to_cpu(gpio->usDataEnRegisterIndex);
  48. i2c.y_clk_reg = le16_to_cpu(gpio->usClkY_RegisterIndex);
  49. i2c.y_data_reg = le16_to_cpu(gpio->usDataY_RegisterIndex);
  50. i2c.a_clk_reg = le16_to_cpu(gpio->usClkA_RegisterIndex);
  51. i2c.a_data_reg = le16_to_cpu(gpio->usDataA_RegisterIndex);
  52. i2c.mask_clk_mask = (1 << gpio->ucClkMaskShift);
  53. i2c.mask_data_mask = (1 << gpio->ucDataMaskShift);
  54. i2c.en_clk_mask = (1 << gpio->ucClkEnShift);
  55. i2c.en_data_mask = (1 << gpio->ucDataEnShift);
  56. i2c.y_clk_mask = (1 << gpio->ucClkY_Shift);
  57. i2c.y_data_mask = (1 << gpio->ucDataY_Shift);
  58. i2c.a_clk_mask = (1 << gpio->ucClkA_Shift);
  59. i2c.a_data_mask = (1 << gpio->ucDataA_Shift);
  60. if (gpio->sucI2cId.sbfAccess.bfHW_Capable)
  61. i2c.hw_capable = true;
  62. else
  63. i2c.hw_capable = false;
  64. if (gpio->sucI2cId.ucAccess == 0xa0)
  65. i2c.mm_i2c = true;
  66. else
  67. i2c.mm_i2c = false;
  68. i2c.i2c_id = gpio->sucI2cId.ucAccess;
  69. if (i2c.mask_clk_reg)
  70. i2c.valid = true;
  71. else
  72. i2c.valid = false;
  73. return i2c;
  74. }
  75. struct amdgpu_i2c_bus_rec amdgpu_atombios_lookup_i2c_gpio(struct amdgpu_device *adev,
  76. uint8_t id)
  77. {
  78. struct atom_context *ctx = adev->mode_info.atom_context;
  79. ATOM_GPIO_I2C_ASSIGMENT *gpio;
  80. struct amdgpu_i2c_bus_rec i2c;
  81. int index = GetIndexIntoMasterTable(DATA, GPIO_I2C_Info);
  82. struct _ATOM_GPIO_I2C_INFO *i2c_info;
  83. uint16_t data_offset, size;
  84. int i, num_indices;
  85. memset(&i2c, 0, sizeof(struct amdgpu_i2c_bus_rec));
  86. i2c.valid = false;
  87. if (amdgpu_atom_parse_data_header(ctx, index, &size, NULL, NULL, &data_offset)) {
  88. i2c_info = (struct _ATOM_GPIO_I2C_INFO *)(ctx->bios + data_offset);
  89. num_indices = (size - sizeof(ATOM_COMMON_TABLE_HEADER)) /
  90. sizeof(ATOM_GPIO_I2C_ASSIGMENT);
  91. gpio = &i2c_info->asGPIO_Info[0];
  92. for (i = 0; i < num_indices; i++) {
  93. amdgpu_atombios_lookup_i2c_gpio_quirks(adev, gpio, i);
  94. if (gpio->sucI2cId.ucAccess == id) {
  95. i2c = amdgpu_atombios_get_bus_rec_for_i2c_gpio(gpio);
  96. break;
  97. }
  98. gpio = (ATOM_GPIO_I2C_ASSIGMENT *)
  99. ((u8 *)gpio + sizeof(ATOM_GPIO_I2C_ASSIGMENT));
  100. }
  101. }
  102. return i2c;
  103. }
  104. void amdgpu_atombios_i2c_init(struct amdgpu_device *adev)
  105. {
  106. struct atom_context *ctx = adev->mode_info.atom_context;
  107. ATOM_GPIO_I2C_ASSIGMENT *gpio;
  108. struct amdgpu_i2c_bus_rec i2c;
  109. int index = GetIndexIntoMasterTable(DATA, GPIO_I2C_Info);
  110. struct _ATOM_GPIO_I2C_INFO *i2c_info;
  111. uint16_t data_offset, size;
  112. int i, num_indices;
  113. char stmp[32];
  114. if (amdgpu_atom_parse_data_header(ctx, index, &size, NULL, NULL, &data_offset)) {
  115. i2c_info = (struct _ATOM_GPIO_I2C_INFO *)(ctx->bios + data_offset);
  116. num_indices = (size - sizeof(ATOM_COMMON_TABLE_HEADER)) /
  117. sizeof(ATOM_GPIO_I2C_ASSIGMENT);
  118. gpio = &i2c_info->asGPIO_Info[0];
  119. for (i = 0; i < num_indices; i++) {
  120. amdgpu_atombios_lookup_i2c_gpio_quirks(adev, gpio, i);
  121. i2c = amdgpu_atombios_get_bus_rec_for_i2c_gpio(gpio);
  122. if (i2c.valid) {
  123. sprintf(stmp, "0x%x", i2c.i2c_id);
  124. adev->i2c_bus[i] = amdgpu_i2c_create(adev->ddev, &i2c, stmp);
  125. }
  126. gpio = (ATOM_GPIO_I2C_ASSIGMENT *)
  127. ((u8 *)gpio + sizeof(ATOM_GPIO_I2C_ASSIGMENT));
  128. }
  129. }
  130. }
  131. struct amdgpu_gpio_rec
  132. amdgpu_atombios_lookup_gpio(struct amdgpu_device *adev,
  133. u8 id)
  134. {
  135. struct atom_context *ctx = adev->mode_info.atom_context;
  136. struct amdgpu_gpio_rec gpio;
  137. int index = GetIndexIntoMasterTable(DATA, GPIO_Pin_LUT);
  138. struct _ATOM_GPIO_PIN_LUT *gpio_info;
  139. ATOM_GPIO_PIN_ASSIGNMENT *pin;
  140. u16 data_offset, size;
  141. int i, num_indices;
  142. memset(&gpio, 0, sizeof(struct amdgpu_gpio_rec));
  143. gpio.valid = false;
  144. if (amdgpu_atom_parse_data_header(ctx, index, &size, NULL, NULL, &data_offset)) {
  145. gpio_info = (struct _ATOM_GPIO_PIN_LUT *)(ctx->bios + data_offset);
  146. num_indices = (size - sizeof(ATOM_COMMON_TABLE_HEADER)) /
  147. sizeof(ATOM_GPIO_PIN_ASSIGNMENT);
  148. pin = gpio_info->asGPIO_Pin;
  149. for (i = 0; i < num_indices; i++) {
  150. if (id == pin->ucGPIO_ID) {
  151. gpio.id = pin->ucGPIO_ID;
  152. gpio.reg = le16_to_cpu(pin->usGpioPin_AIndex);
  153. gpio.shift = pin->ucGpioPinBitShift;
  154. gpio.mask = (1 << pin->ucGpioPinBitShift);
  155. gpio.valid = true;
  156. break;
  157. }
  158. pin = (ATOM_GPIO_PIN_ASSIGNMENT *)
  159. ((u8 *)pin + sizeof(ATOM_GPIO_PIN_ASSIGNMENT));
  160. }
  161. }
  162. return gpio;
  163. }
  164. static struct amdgpu_hpd
  165. amdgpu_atombios_get_hpd_info_from_gpio(struct amdgpu_device *adev,
  166. struct amdgpu_gpio_rec *gpio)
  167. {
  168. struct amdgpu_hpd hpd;
  169. u32 reg;
  170. memset(&hpd, 0, sizeof(struct amdgpu_hpd));
  171. reg = amdgpu_display_hpd_get_gpio_reg(adev);
  172. hpd.gpio = *gpio;
  173. if (gpio->reg == reg) {
  174. switch(gpio->mask) {
  175. case (1 << 0):
  176. hpd.hpd = AMDGPU_HPD_1;
  177. break;
  178. case (1 << 8):
  179. hpd.hpd = AMDGPU_HPD_2;
  180. break;
  181. case (1 << 16):
  182. hpd.hpd = AMDGPU_HPD_3;
  183. break;
  184. case (1 << 24):
  185. hpd.hpd = AMDGPU_HPD_4;
  186. break;
  187. case (1 << 26):
  188. hpd.hpd = AMDGPU_HPD_5;
  189. break;
  190. case (1 << 28):
  191. hpd.hpd = AMDGPU_HPD_6;
  192. break;
  193. default:
  194. hpd.hpd = AMDGPU_HPD_NONE;
  195. break;
  196. }
  197. } else
  198. hpd.hpd = AMDGPU_HPD_NONE;
  199. return hpd;
  200. }
  201. static const int object_connector_convert[] = {
  202. DRM_MODE_CONNECTOR_Unknown,
  203. DRM_MODE_CONNECTOR_DVII,
  204. DRM_MODE_CONNECTOR_DVII,
  205. DRM_MODE_CONNECTOR_DVID,
  206. DRM_MODE_CONNECTOR_DVID,
  207. DRM_MODE_CONNECTOR_VGA,
  208. DRM_MODE_CONNECTOR_Composite,
  209. DRM_MODE_CONNECTOR_SVIDEO,
  210. DRM_MODE_CONNECTOR_Unknown,
  211. DRM_MODE_CONNECTOR_Unknown,
  212. DRM_MODE_CONNECTOR_9PinDIN,
  213. DRM_MODE_CONNECTOR_Unknown,
  214. DRM_MODE_CONNECTOR_HDMIA,
  215. DRM_MODE_CONNECTOR_HDMIB,
  216. DRM_MODE_CONNECTOR_LVDS,
  217. DRM_MODE_CONNECTOR_9PinDIN,
  218. DRM_MODE_CONNECTOR_Unknown,
  219. DRM_MODE_CONNECTOR_Unknown,
  220. DRM_MODE_CONNECTOR_Unknown,
  221. DRM_MODE_CONNECTOR_DisplayPort,
  222. DRM_MODE_CONNECTOR_eDP,
  223. DRM_MODE_CONNECTOR_Unknown
  224. };
  225. bool amdgpu_atombios_has_dce_engine_info(struct amdgpu_device *adev)
  226. {
  227. struct amdgpu_mode_info *mode_info = &adev->mode_info;
  228. struct atom_context *ctx = mode_info->atom_context;
  229. int index = GetIndexIntoMasterTable(DATA, Object_Header);
  230. u16 size, data_offset;
  231. u8 frev, crev;
  232. ATOM_DISPLAY_OBJECT_PATH_TABLE *path_obj;
  233. ATOM_OBJECT_HEADER *obj_header;
  234. if (!amdgpu_atom_parse_data_header(ctx, index, &size, &frev, &crev, &data_offset))
  235. return false;
  236. if (crev < 2)
  237. return false;
  238. obj_header = (ATOM_OBJECT_HEADER *) (ctx->bios + data_offset);
  239. path_obj = (ATOM_DISPLAY_OBJECT_PATH_TABLE *)
  240. (ctx->bios + data_offset +
  241. le16_to_cpu(obj_header->usDisplayPathTableOffset));
  242. if (path_obj->ucNumOfDispPath)
  243. return true;
  244. else
  245. return false;
  246. }
  247. bool amdgpu_atombios_get_connector_info_from_object_table(struct amdgpu_device *adev)
  248. {
  249. struct amdgpu_mode_info *mode_info = &adev->mode_info;
  250. struct atom_context *ctx = mode_info->atom_context;
  251. int index = GetIndexIntoMasterTable(DATA, Object_Header);
  252. u16 size, data_offset;
  253. u8 frev, crev;
  254. ATOM_CONNECTOR_OBJECT_TABLE *con_obj;
  255. ATOM_ENCODER_OBJECT_TABLE *enc_obj;
  256. ATOM_OBJECT_TABLE *router_obj;
  257. ATOM_DISPLAY_OBJECT_PATH_TABLE *path_obj;
  258. ATOM_OBJECT_HEADER *obj_header;
  259. int i, j, k, path_size, device_support;
  260. int connector_type;
  261. u16 conn_id, connector_object_id;
  262. struct amdgpu_i2c_bus_rec ddc_bus;
  263. struct amdgpu_router router;
  264. struct amdgpu_gpio_rec gpio;
  265. struct amdgpu_hpd hpd;
  266. if (!amdgpu_atom_parse_data_header(ctx, index, &size, &frev, &crev, &data_offset))
  267. return false;
  268. if (crev < 2)
  269. return false;
  270. obj_header = (ATOM_OBJECT_HEADER *) (ctx->bios + data_offset);
  271. path_obj = (ATOM_DISPLAY_OBJECT_PATH_TABLE *)
  272. (ctx->bios + data_offset +
  273. le16_to_cpu(obj_header->usDisplayPathTableOffset));
  274. con_obj = (ATOM_CONNECTOR_OBJECT_TABLE *)
  275. (ctx->bios + data_offset +
  276. le16_to_cpu(obj_header->usConnectorObjectTableOffset));
  277. enc_obj = (ATOM_ENCODER_OBJECT_TABLE *)
  278. (ctx->bios + data_offset +
  279. le16_to_cpu(obj_header->usEncoderObjectTableOffset));
  280. router_obj = (ATOM_OBJECT_TABLE *)
  281. (ctx->bios + data_offset +
  282. le16_to_cpu(obj_header->usRouterObjectTableOffset));
  283. device_support = le16_to_cpu(obj_header->usDeviceSupport);
  284. path_size = 0;
  285. for (i = 0; i < path_obj->ucNumOfDispPath; i++) {
  286. uint8_t *addr = (uint8_t *) path_obj->asDispPath;
  287. ATOM_DISPLAY_OBJECT_PATH *path;
  288. addr += path_size;
  289. path = (ATOM_DISPLAY_OBJECT_PATH *) addr;
  290. path_size += le16_to_cpu(path->usSize);
  291. if (device_support & le16_to_cpu(path->usDeviceTag)) {
  292. uint8_t con_obj_id, con_obj_num, con_obj_type;
  293. con_obj_id =
  294. (le16_to_cpu(path->usConnObjectId) & OBJECT_ID_MASK)
  295. >> OBJECT_ID_SHIFT;
  296. con_obj_num =
  297. (le16_to_cpu(path->usConnObjectId) & ENUM_ID_MASK)
  298. >> ENUM_ID_SHIFT;
  299. con_obj_type =
  300. (le16_to_cpu(path->usConnObjectId) &
  301. OBJECT_TYPE_MASK) >> OBJECT_TYPE_SHIFT;
  302. /* Skip TV/CV support */
  303. if ((le16_to_cpu(path->usDeviceTag) ==
  304. ATOM_DEVICE_TV1_SUPPORT) ||
  305. (le16_to_cpu(path->usDeviceTag) ==
  306. ATOM_DEVICE_CV_SUPPORT))
  307. continue;
  308. if (con_obj_id >= ARRAY_SIZE(object_connector_convert)) {
  309. DRM_ERROR("invalid con_obj_id %d for device tag 0x%04x\n",
  310. con_obj_id, le16_to_cpu(path->usDeviceTag));
  311. continue;
  312. }
  313. connector_type =
  314. object_connector_convert[con_obj_id];
  315. connector_object_id = con_obj_id;
  316. if (connector_type == DRM_MODE_CONNECTOR_Unknown)
  317. continue;
  318. router.ddc_valid = false;
  319. router.cd_valid = false;
  320. for (j = 0; j < ((le16_to_cpu(path->usSize) - 8) / 2); j++) {
  321. uint8_t grph_obj_id, grph_obj_num, grph_obj_type;
  322. grph_obj_id =
  323. (le16_to_cpu(path->usGraphicObjIds[j]) &
  324. OBJECT_ID_MASK) >> OBJECT_ID_SHIFT;
  325. grph_obj_num =
  326. (le16_to_cpu(path->usGraphicObjIds[j]) &
  327. ENUM_ID_MASK) >> ENUM_ID_SHIFT;
  328. grph_obj_type =
  329. (le16_to_cpu(path->usGraphicObjIds[j]) &
  330. OBJECT_TYPE_MASK) >> OBJECT_TYPE_SHIFT;
  331. if (grph_obj_type == GRAPH_OBJECT_TYPE_ENCODER) {
  332. for (k = 0; k < enc_obj->ucNumberOfObjects; k++) {
  333. u16 encoder_obj = le16_to_cpu(enc_obj->asObjects[k].usObjectID);
  334. if (le16_to_cpu(path->usGraphicObjIds[j]) == encoder_obj) {
  335. ATOM_COMMON_RECORD_HEADER *record = (ATOM_COMMON_RECORD_HEADER *)
  336. (ctx->bios + data_offset +
  337. le16_to_cpu(enc_obj->asObjects[k].usRecordOffset));
  338. ATOM_ENCODER_CAP_RECORD *cap_record;
  339. u16 caps = 0;
  340. while (record->ucRecordSize > 0 &&
  341. record->ucRecordType > 0 &&
  342. record->ucRecordType <= ATOM_MAX_OBJECT_RECORD_NUMBER) {
  343. switch (record->ucRecordType) {
  344. case ATOM_ENCODER_CAP_RECORD_TYPE:
  345. cap_record =(ATOM_ENCODER_CAP_RECORD *)
  346. record;
  347. caps = le16_to_cpu(cap_record->usEncoderCap);
  348. break;
  349. }
  350. record = (ATOM_COMMON_RECORD_HEADER *)
  351. ((char *)record + record->ucRecordSize);
  352. }
  353. amdgpu_display_add_encoder(adev, encoder_obj,
  354. le16_to_cpu(path->usDeviceTag),
  355. caps);
  356. }
  357. }
  358. } else if (grph_obj_type == GRAPH_OBJECT_TYPE_ROUTER) {
  359. for (k = 0; k < router_obj->ucNumberOfObjects; k++) {
  360. u16 router_obj_id = le16_to_cpu(router_obj->asObjects[k].usObjectID);
  361. if (le16_to_cpu(path->usGraphicObjIds[j]) == router_obj_id) {
  362. ATOM_COMMON_RECORD_HEADER *record = (ATOM_COMMON_RECORD_HEADER *)
  363. (ctx->bios + data_offset +
  364. le16_to_cpu(router_obj->asObjects[k].usRecordOffset));
  365. ATOM_I2C_RECORD *i2c_record;
  366. ATOM_I2C_ID_CONFIG_ACCESS *i2c_config;
  367. ATOM_ROUTER_DDC_PATH_SELECT_RECORD *ddc_path;
  368. ATOM_ROUTER_DATA_CLOCK_PATH_SELECT_RECORD *cd_path;
  369. ATOM_SRC_DST_TABLE_FOR_ONE_OBJECT *router_src_dst_table =
  370. (ATOM_SRC_DST_TABLE_FOR_ONE_OBJECT *)
  371. (ctx->bios + data_offset +
  372. le16_to_cpu(router_obj->asObjects[k].usSrcDstTableOffset));
  373. u8 *num_dst_objs = (u8 *)
  374. ((u8 *)router_src_dst_table + 1 +
  375. (router_src_dst_table->ucNumberOfSrc * 2));
  376. u16 *dst_objs = (u16 *)(num_dst_objs + 1);
  377. int enum_id;
  378. router.router_id = router_obj_id;
  379. for (enum_id = 0; enum_id < (*num_dst_objs); enum_id++) {
  380. if (le16_to_cpu(path->usConnObjectId) ==
  381. le16_to_cpu(dst_objs[enum_id]))
  382. break;
  383. }
  384. while (record->ucRecordSize > 0 &&
  385. record->ucRecordType > 0 &&
  386. record->ucRecordType <= ATOM_MAX_OBJECT_RECORD_NUMBER) {
  387. switch (record->ucRecordType) {
  388. case ATOM_I2C_RECORD_TYPE:
  389. i2c_record =
  390. (ATOM_I2C_RECORD *)
  391. record;
  392. i2c_config =
  393. (ATOM_I2C_ID_CONFIG_ACCESS *)
  394. &i2c_record->sucI2cId;
  395. router.i2c_info =
  396. amdgpu_atombios_lookup_i2c_gpio(adev,
  397. i2c_config->
  398. ucAccess);
  399. router.i2c_addr = i2c_record->ucI2CAddr >> 1;
  400. break;
  401. case ATOM_ROUTER_DDC_PATH_SELECT_RECORD_TYPE:
  402. ddc_path = (ATOM_ROUTER_DDC_PATH_SELECT_RECORD *)
  403. record;
  404. router.ddc_valid = true;
  405. router.ddc_mux_type = ddc_path->ucMuxType;
  406. router.ddc_mux_control_pin = ddc_path->ucMuxControlPin;
  407. router.ddc_mux_state = ddc_path->ucMuxState[enum_id];
  408. break;
  409. case ATOM_ROUTER_DATA_CLOCK_PATH_SELECT_RECORD_TYPE:
  410. cd_path = (ATOM_ROUTER_DATA_CLOCK_PATH_SELECT_RECORD *)
  411. record;
  412. router.cd_valid = true;
  413. router.cd_mux_type = cd_path->ucMuxType;
  414. router.cd_mux_control_pin = cd_path->ucMuxControlPin;
  415. router.cd_mux_state = cd_path->ucMuxState[enum_id];
  416. break;
  417. }
  418. record = (ATOM_COMMON_RECORD_HEADER *)
  419. ((char *)record + record->ucRecordSize);
  420. }
  421. }
  422. }
  423. }
  424. }
  425. /* look up gpio for ddc, hpd */
  426. ddc_bus.valid = false;
  427. hpd.hpd = AMDGPU_HPD_NONE;
  428. if ((le16_to_cpu(path->usDeviceTag) &
  429. (ATOM_DEVICE_TV_SUPPORT | ATOM_DEVICE_CV_SUPPORT)) == 0) {
  430. for (j = 0; j < con_obj->ucNumberOfObjects; j++) {
  431. if (le16_to_cpu(path->usConnObjectId) ==
  432. le16_to_cpu(con_obj->asObjects[j].
  433. usObjectID)) {
  434. ATOM_COMMON_RECORD_HEADER
  435. *record =
  436. (ATOM_COMMON_RECORD_HEADER
  437. *)
  438. (ctx->bios + data_offset +
  439. le16_to_cpu(con_obj->
  440. asObjects[j].
  441. usRecordOffset));
  442. ATOM_I2C_RECORD *i2c_record;
  443. ATOM_HPD_INT_RECORD *hpd_record;
  444. ATOM_I2C_ID_CONFIG_ACCESS *i2c_config;
  445. while (record->ucRecordSize > 0 &&
  446. record->ucRecordType > 0 &&
  447. record->ucRecordType <= ATOM_MAX_OBJECT_RECORD_NUMBER) {
  448. switch (record->ucRecordType) {
  449. case ATOM_I2C_RECORD_TYPE:
  450. i2c_record =
  451. (ATOM_I2C_RECORD *)
  452. record;
  453. i2c_config =
  454. (ATOM_I2C_ID_CONFIG_ACCESS *)
  455. &i2c_record->sucI2cId;
  456. ddc_bus = amdgpu_atombios_lookup_i2c_gpio(adev,
  457. i2c_config->
  458. ucAccess);
  459. break;
  460. case ATOM_HPD_INT_RECORD_TYPE:
  461. hpd_record =
  462. (ATOM_HPD_INT_RECORD *)
  463. record;
  464. gpio = amdgpu_atombios_lookup_gpio(adev,
  465. hpd_record->ucHPDIntGPIOID);
  466. hpd = amdgpu_atombios_get_hpd_info_from_gpio(adev, &gpio);
  467. hpd.plugged_state = hpd_record->ucPlugged_PinState;
  468. break;
  469. }
  470. record =
  471. (ATOM_COMMON_RECORD_HEADER
  472. *) ((char *)record
  473. +
  474. record->
  475. ucRecordSize);
  476. }
  477. break;
  478. }
  479. }
  480. }
  481. /* needed for aux chan transactions */
  482. ddc_bus.hpd = hpd.hpd;
  483. conn_id = le16_to_cpu(path->usConnObjectId);
  484. amdgpu_display_add_connector(adev,
  485. conn_id,
  486. le16_to_cpu(path->usDeviceTag),
  487. connector_type, &ddc_bus,
  488. connector_object_id,
  489. &hpd,
  490. &router);
  491. }
  492. }
  493. amdgpu_link_encoder_connector(adev->ddev);
  494. return true;
  495. }
  496. union firmware_info {
  497. ATOM_FIRMWARE_INFO info;
  498. ATOM_FIRMWARE_INFO_V1_2 info_12;
  499. ATOM_FIRMWARE_INFO_V1_3 info_13;
  500. ATOM_FIRMWARE_INFO_V1_4 info_14;
  501. ATOM_FIRMWARE_INFO_V2_1 info_21;
  502. ATOM_FIRMWARE_INFO_V2_2 info_22;
  503. };
  504. int amdgpu_atombios_get_clock_info(struct amdgpu_device *adev)
  505. {
  506. struct amdgpu_mode_info *mode_info = &adev->mode_info;
  507. int index = GetIndexIntoMasterTable(DATA, FirmwareInfo);
  508. uint8_t frev, crev;
  509. uint16_t data_offset;
  510. int ret = -EINVAL;
  511. if (amdgpu_atom_parse_data_header(mode_info->atom_context, index, NULL,
  512. &frev, &crev, &data_offset)) {
  513. int i;
  514. struct amdgpu_pll *ppll = &adev->clock.ppll[0];
  515. struct amdgpu_pll *spll = &adev->clock.spll;
  516. struct amdgpu_pll *mpll = &adev->clock.mpll;
  517. union firmware_info *firmware_info =
  518. (union firmware_info *)(mode_info->atom_context->bios +
  519. data_offset);
  520. /* pixel clocks */
  521. ppll->reference_freq =
  522. le16_to_cpu(firmware_info->info.usReferenceClock);
  523. ppll->reference_div = 0;
  524. ppll->pll_out_min =
  525. le32_to_cpu(firmware_info->info_12.ulMinPixelClockPLL_Output);
  526. ppll->pll_out_max =
  527. le32_to_cpu(firmware_info->info.ulMaxPixelClockPLL_Output);
  528. ppll->lcd_pll_out_min =
  529. le16_to_cpu(firmware_info->info_14.usLcdMinPixelClockPLL_Output) * 100;
  530. if (ppll->lcd_pll_out_min == 0)
  531. ppll->lcd_pll_out_min = ppll->pll_out_min;
  532. ppll->lcd_pll_out_max =
  533. le16_to_cpu(firmware_info->info_14.usLcdMaxPixelClockPLL_Output) * 100;
  534. if (ppll->lcd_pll_out_max == 0)
  535. ppll->lcd_pll_out_max = ppll->pll_out_max;
  536. if (ppll->pll_out_min == 0)
  537. ppll->pll_out_min = 64800;
  538. ppll->pll_in_min =
  539. le16_to_cpu(firmware_info->info.usMinPixelClockPLL_Input);
  540. ppll->pll_in_max =
  541. le16_to_cpu(firmware_info->info.usMaxPixelClockPLL_Input);
  542. ppll->min_post_div = 2;
  543. ppll->max_post_div = 0x7f;
  544. ppll->min_frac_feedback_div = 0;
  545. ppll->max_frac_feedback_div = 9;
  546. ppll->min_ref_div = 2;
  547. ppll->max_ref_div = 0x3ff;
  548. ppll->min_feedback_div = 4;
  549. ppll->max_feedback_div = 0xfff;
  550. ppll->best_vco = 0;
  551. for (i = 1; i < AMDGPU_MAX_PPLL; i++)
  552. adev->clock.ppll[i] = *ppll;
  553. /* system clock */
  554. spll->reference_freq =
  555. le16_to_cpu(firmware_info->info_21.usCoreReferenceClock);
  556. spll->reference_div = 0;
  557. spll->pll_out_min =
  558. le16_to_cpu(firmware_info->info.usMinEngineClockPLL_Output);
  559. spll->pll_out_max =
  560. le32_to_cpu(firmware_info->info.ulMaxEngineClockPLL_Output);
  561. /* ??? */
  562. if (spll->pll_out_min == 0)
  563. spll->pll_out_min = 64800;
  564. spll->pll_in_min =
  565. le16_to_cpu(firmware_info->info.usMinEngineClockPLL_Input);
  566. spll->pll_in_max =
  567. le16_to_cpu(firmware_info->info.usMaxEngineClockPLL_Input);
  568. spll->min_post_div = 1;
  569. spll->max_post_div = 1;
  570. spll->min_ref_div = 2;
  571. spll->max_ref_div = 0xff;
  572. spll->min_feedback_div = 4;
  573. spll->max_feedback_div = 0xff;
  574. spll->best_vco = 0;
  575. /* memory clock */
  576. mpll->reference_freq =
  577. le16_to_cpu(firmware_info->info_21.usMemoryReferenceClock);
  578. mpll->reference_div = 0;
  579. mpll->pll_out_min =
  580. le16_to_cpu(firmware_info->info.usMinMemoryClockPLL_Output);
  581. mpll->pll_out_max =
  582. le32_to_cpu(firmware_info->info.ulMaxMemoryClockPLL_Output);
  583. /* ??? */
  584. if (mpll->pll_out_min == 0)
  585. mpll->pll_out_min = 64800;
  586. mpll->pll_in_min =
  587. le16_to_cpu(firmware_info->info.usMinMemoryClockPLL_Input);
  588. mpll->pll_in_max =
  589. le16_to_cpu(firmware_info->info.usMaxMemoryClockPLL_Input);
  590. adev->clock.default_sclk =
  591. le32_to_cpu(firmware_info->info.ulDefaultEngineClock);
  592. adev->clock.default_mclk =
  593. le32_to_cpu(firmware_info->info.ulDefaultMemoryClock);
  594. mpll->min_post_div = 1;
  595. mpll->max_post_div = 1;
  596. mpll->min_ref_div = 2;
  597. mpll->max_ref_div = 0xff;
  598. mpll->min_feedback_div = 4;
  599. mpll->max_feedback_div = 0xff;
  600. mpll->best_vco = 0;
  601. /* disp clock */
  602. adev->clock.default_dispclk =
  603. le32_to_cpu(firmware_info->info_21.ulDefaultDispEngineClkFreq);
  604. /* set a reasonable default for DP */
  605. if (adev->clock.default_dispclk < 53900) {
  606. DRM_INFO("Changing default dispclk from %dMhz to 600Mhz\n",
  607. adev->clock.default_dispclk / 100);
  608. adev->clock.default_dispclk = 60000;
  609. }
  610. adev->clock.dp_extclk =
  611. le16_to_cpu(firmware_info->info_21.usUniphyDPModeExtClkFreq);
  612. adev->clock.current_dispclk = adev->clock.default_dispclk;
  613. adev->clock.max_pixel_clock = le16_to_cpu(firmware_info->info.usMaxPixelClock);
  614. if (adev->clock.max_pixel_clock == 0)
  615. adev->clock.max_pixel_clock = 40000;
  616. /* not technically a clock, but... */
  617. adev->mode_info.firmware_flags =
  618. le16_to_cpu(firmware_info->info.usFirmwareCapability.susAccess);
  619. ret = 0;
  620. }
  621. adev->pm.current_sclk = adev->clock.default_sclk;
  622. adev->pm.current_mclk = adev->clock.default_mclk;
  623. return ret;
  624. }
  625. union gfx_info {
  626. ATOM_GFX_INFO_V2_1 info;
  627. };
  628. int amdgpu_atombios_get_gfx_info(struct amdgpu_device *adev)
  629. {
  630. struct amdgpu_mode_info *mode_info = &adev->mode_info;
  631. int index = GetIndexIntoMasterTable(DATA, GFX_Info);
  632. uint8_t frev, crev;
  633. uint16_t data_offset;
  634. int ret = -EINVAL;
  635. if (amdgpu_atom_parse_data_header(mode_info->atom_context, index, NULL,
  636. &frev, &crev, &data_offset)) {
  637. union gfx_info *gfx_info = (union gfx_info *)
  638. (mode_info->atom_context->bios + data_offset);
  639. adev->gfx.config.max_shader_engines = gfx_info->info.max_shader_engines;
  640. adev->gfx.config.max_tile_pipes = gfx_info->info.max_tile_pipes;
  641. adev->gfx.config.max_cu_per_sh = gfx_info->info.max_cu_per_sh;
  642. adev->gfx.config.max_sh_per_se = gfx_info->info.max_sh_per_se;
  643. adev->gfx.config.max_backends_per_se = gfx_info->info.max_backends_per_se;
  644. adev->gfx.config.max_texture_channel_caches =
  645. gfx_info->info.max_texture_channel_caches;
  646. ret = 0;
  647. }
  648. return ret;
  649. }
  650. union igp_info {
  651. struct _ATOM_INTEGRATED_SYSTEM_INFO info;
  652. struct _ATOM_INTEGRATED_SYSTEM_INFO_V2 info_2;
  653. struct _ATOM_INTEGRATED_SYSTEM_INFO_V6 info_6;
  654. struct _ATOM_INTEGRATED_SYSTEM_INFO_V1_7 info_7;
  655. struct _ATOM_INTEGRATED_SYSTEM_INFO_V1_8 info_8;
  656. struct _ATOM_INTEGRATED_SYSTEM_INFO_V1_9 info_9;
  657. };
  658. static void amdgpu_atombios_get_igp_ss_overrides(struct amdgpu_device *adev,
  659. struct amdgpu_atom_ss *ss,
  660. int id)
  661. {
  662. struct amdgpu_mode_info *mode_info = &adev->mode_info;
  663. int index = GetIndexIntoMasterTable(DATA, IntegratedSystemInfo);
  664. u16 data_offset, size;
  665. union igp_info *igp_info;
  666. u8 frev, crev;
  667. u16 percentage = 0, rate = 0;
  668. /* get any igp specific overrides */
  669. if (amdgpu_atom_parse_data_header(mode_info->atom_context, index, &size,
  670. &frev, &crev, &data_offset)) {
  671. igp_info = (union igp_info *)
  672. (mode_info->atom_context->bios + data_offset);
  673. switch (crev) {
  674. case 6:
  675. switch (id) {
  676. case ASIC_INTERNAL_SS_ON_TMDS:
  677. percentage = le16_to_cpu(igp_info->info_6.usDVISSPercentage);
  678. rate = le16_to_cpu(igp_info->info_6.usDVISSpreadRateIn10Hz);
  679. break;
  680. case ASIC_INTERNAL_SS_ON_HDMI:
  681. percentage = le16_to_cpu(igp_info->info_6.usHDMISSPercentage);
  682. rate = le16_to_cpu(igp_info->info_6.usHDMISSpreadRateIn10Hz);
  683. break;
  684. case ASIC_INTERNAL_SS_ON_LVDS:
  685. percentage = le16_to_cpu(igp_info->info_6.usLvdsSSPercentage);
  686. rate = le16_to_cpu(igp_info->info_6.usLvdsSSpreadRateIn10Hz);
  687. break;
  688. }
  689. break;
  690. case 7:
  691. switch (id) {
  692. case ASIC_INTERNAL_SS_ON_TMDS:
  693. percentage = le16_to_cpu(igp_info->info_7.usDVISSPercentage);
  694. rate = le16_to_cpu(igp_info->info_7.usDVISSpreadRateIn10Hz);
  695. break;
  696. case ASIC_INTERNAL_SS_ON_HDMI:
  697. percentage = le16_to_cpu(igp_info->info_7.usHDMISSPercentage);
  698. rate = le16_to_cpu(igp_info->info_7.usHDMISSpreadRateIn10Hz);
  699. break;
  700. case ASIC_INTERNAL_SS_ON_LVDS:
  701. percentage = le16_to_cpu(igp_info->info_7.usLvdsSSPercentage);
  702. rate = le16_to_cpu(igp_info->info_7.usLvdsSSpreadRateIn10Hz);
  703. break;
  704. }
  705. break;
  706. case 8:
  707. switch (id) {
  708. case ASIC_INTERNAL_SS_ON_TMDS:
  709. percentage = le16_to_cpu(igp_info->info_8.usDVISSPercentage);
  710. rate = le16_to_cpu(igp_info->info_8.usDVISSpreadRateIn10Hz);
  711. break;
  712. case ASIC_INTERNAL_SS_ON_HDMI:
  713. percentage = le16_to_cpu(igp_info->info_8.usHDMISSPercentage);
  714. rate = le16_to_cpu(igp_info->info_8.usHDMISSpreadRateIn10Hz);
  715. break;
  716. case ASIC_INTERNAL_SS_ON_LVDS:
  717. percentage = le16_to_cpu(igp_info->info_8.usLvdsSSPercentage);
  718. rate = le16_to_cpu(igp_info->info_8.usLvdsSSpreadRateIn10Hz);
  719. break;
  720. }
  721. break;
  722. case 9:
  723. switch (id) {
  724. case ASIC_INTERNAL_SS_ON_TMDS:
  725. percentage = le16_to_cpu(igp_info->info_9.usDVISSPercentage);
  726. rate = le16_to_cpu(igp_info->info_9.usDVISSpreadRateIn10Hz);
  727. break;
  728. case ASIC_INTERNAL_SS_ON_HDMI:
  729. percentage = le16_to_cpu(igp_info->info_9.usHDMISSPercentage);
  730. rate = le16_to_cpu(igp_info->info_9.usHDMISSpreadRateIn10Hz);
  731. break;
  732. case ASIC_INTERNAL_SS_ON_LVDS:
  733. percentage = le16_to_cpu(igp_info->info_9.usLvdsSSPercentage);
  734. rate = le16_to_cpu(igp_info->info_9.usLvdsSSpreadRateIn10Hz);
  735. break;
  736. }
  737. break;
  738. default:
  739. DRM_ERROR("Unsupported IGP table: %d %d\n", frev, crev);
  740. break;
  741. }
  742. if (percentage)
  743. ss->percentage = percentage;
  744. if (rate)
  745. ss->rate = rate;
  746. }
  747. }
  748. union asic_ss_info {
  749. struct _ATOM_ASIC_INTERNAL_SS_INFO info;
  750. struct _ATOM_ASIC_INTERNAL_SS_INFO_V2 info_2;
  751. struct _ATOM_ASIC_INTERNAL_SS_INFO_V3 info_3;
  752. };
  753. union asic_ss_assignment {
  754. struct _ATOM_ASIC_SS_ASSIGNMENT v1;
  755. struct _ATOM_ASIC_SS_ASSIGNMENT_V2 v2;
  756. struct _ATOM_ASIC_SS_ASSIGNMENT_V3 v3;
  757. };
  758. bool amdgpu_atombios_get_asic_ss_info(struct amdgpu_device *adev,
  759. struct amdgpu_atom_ss *ss,
  760. int id, u32 clock)
  761. {
  762. struct amdgpu_mode_info *mode_info = &adev->mode_info;
  763. int index = GetIndexIntoMasterTable(DATA, ASIC_InternalSS_Info);
  764. uint16_t data_offset, size;
  765. union asic_ss_info *ss_info;
  766. union asic_ss_assignment *ss_assign;
  767. uint8_t frev, crev;
  768. int i, num_indices;
  769. if (id == ASIC_INTERNAL_MEMORY_SS) {
  770. if (!(adev->mode_info.firmware_flags & ATOM_BIOS_INFO_MEMORY_CLOCK_SS_SUPPORT))
  771. return false;
  772. }
  773. if (id == ASIC_INTERNAL_ENGINE_SS) {
  774. if (!(adev->mode_info.firmware_flags & ATOM_BIOS_INFO_ENGINE_CLOCK_SS_SUPPORT))
  775. return false;
  776. }
  777. memset(ss, 0, sizeof(struct amdgpu_atom_ss));
  778. if (amdgpu_atom_parse_data_header(mode_info->atom_context, index, &size,
  779. &frev, &crev, &data_offset)) {
  780. ss_info =
  781. (union asic_ss_info *)(mode_info->atom_context->bios + data_offset);
  782. switch (frev) {
  783. case 1:
  784. num_indices = (size - sizeof(ATOM_COMMON_TABLE_HEADER)) /
  785. sizeof(ATOM_ASIC_SS_ASSIGNMENT);
  786. ss_assign = (union asic_ss_assignment *)((u8 *)&ss_info->info.asSpreadSpectrum[0]);
  787. for (i = 0; i < num_indices; i++) {
  788. if ((ss_assign->v1.ucClockIndication == id) &&
  789. (clock <= le32_to_cpu(ss_assign->v1.ulTargetClockRange))) {
  790. ss->percentage =
  791. le16_to_cpu(ss_assign->v1.usSpreadSpectrumPercentage);
  792. ss->type = ss_assign->v1.ucSpreadSpectrumMode;
  793. ss->rate = le16_to_cpu(ss_assign->v1.usSpreadRateInKhz);
  794. ss->percentage_divider = 100;
  795. return true;
  796. }
  797. ss_assign = (union asic_ss_assignment *)
  798. ((u8 *)ss_assign + sizeof(ATOM_ASIC_SS_ASSIGNMENT));
  799. }
  800. break;
  801. case 2:
  802. num_indices = (size - sizeof(ATOM_COMMON_TABLE_HEADER)) /
  803. sizeof(ATOM_ASIC_SS_ASSIGNMENT_V2);
  804. ss_assign = (union asic_ss_assignment *)((u8 *)&ss_info->info_2.asSpreadSpectrum[0]);
  805. for (i = 0; i < num_indices; i++) {
  806. if ((ss_assign->v2.ucClockIndication == id) &&
  807. (clock <= le32_to_cpu(ss_assign->v2.ulTargetClockRange))) {
  808. ss->percentage =
  809. le16_to_cpu(ss_assign->v2.usSpreadSpectrumPercentage);
  810. ss->type = ss_assign->v2.ucSpreadSpectrumMode;
  811. ss->rate = le16_to_cpu(ss_assign->v2.usSpreadRateIn10Hz);
  812. ss->percentage_divider = 100;
  813. if ((crev == 2) &&
  814. ((id == ASIC_INTERNAL_ENGINE_SS) ||
  815. (id == ASIC_INTERNAL_MEMORY_SS)))
  816. ss->rate /= 100;
  817. return true;
  818. }
  819. ss_assign = (union asic_ss_assignment *)
  820. ((u8 *)ss_assign + sizeof(ATOM_ASIC_SS_ASSIGNMENT_V2));
  821. }
  822. break;
  823. case 3:
  824. num_indices = (size - sizeof(ATOM_COMMON_TABLE_HEADER)) /
  825. sizeof(ATOM_ASIC_SS_ASSIGNMENT_V3);
  826. ss_assign = (union asic_ss_assignment *)((u8 *)&ss_info->info_3.asSpreadSpectrum[0]);
  827. for (i = 0; i < num_indices; i++) {
  828. if ((ss_assign->v3.ucClockIndication == id) &&
  829. (clock <= le32_to_cpu(ss_assign->v3.ulTargetClockRange))) {
  830. ss->percentage =
  831. le16_to_cpu(ss_assign->v3.usSpreadSpectrumPercentage);
  832. ss->type = ss_assign->v3.ucSpreadSpectrumMode;
  833. ss->rate = le16_to_cpu(ss_assign->v3.usSpreadRateIn10Hz);
  834. if (ss_assign->v3.ucSpreadSpectrumMode &
  835. SS_MODE_V3_PERCENTAGE_DIV_BY_1000_MASK)
  836. ss->percentage_divider = 1000;
  837. else
  838. ss->percentage_divider = 100;
  839. if ((id == ASIC_INTERNAL_ENGINE_SS) ||
  840. (id == ASIC_INTERNAL_MEMORY_SS))
  841. ss->rate /= 100;
  842. if (adev->flags & AMD_IS_APU)
  843. amdgpu_atombios_get_igp_ss_overrides(adev, ss, id);
  844. return true;
  845. }
  846. ss_assign = (union asic_ss_assignment *)
  847. ((u8 *)ss_assign + sizeof(ATOM_ASIC_SS_ASSIGNMENT_V3));
  848. }
  849. break;
  850. default:
  851. DRM_ERROR("Unsupported ASIC_InternalSS_Info table: %d %d\n", frev, crev);
  852. break;
  853. }
  854. }
  855. return false;
  856. }
  857. union get_clock_dividers {
  858. struct _COMPUTE_MEMORY_ENGINE_PLL_PARAMETERS v1;
  859. struct _COMPUTE_MEMORY_ENGINE_PLL_PARAMETERS_V2 v2;
  860. struct _COMPUTE_MEMORY_ENGINE_PLL_PARAMETERS_V3 v3;
  861. struct _COMPUTE_MEMORY_ENGINE_PLL_PARAMETERS_V4 v4;
  862. struct _COMPUTE_MEMORY_ENGINE_PLL_PARAMETERS_V5 v5;
  863. struct _COMPUTE_GPU_CLOCK_INPUT_PARAMETERS_V1_6 v6_in;
  864. struct _COMPUTE_GPU_CLOCK_OUTPUT_PARAMETERS_V1_6 v6_out;
  865. };
  866. int amdgpu_atombios_get_clock_dividers(struct amdgpu_device *adev,
  867. u8 clock_type,
  868. u32 clock,
  869. bool strobe_mode,
  870. struct atom_clock_dividers *dividers)
  871. {
  872. union get_clock_dividers args;
  873. int index = GetIndexIntoMasterTable(COMMAND, ComputeMemoryEnginePLL);
  874. u8 frev, crev;
  875. memset(&args, 0, sizeof(args));
  876. memset(dividers, 0, sizeof(struct atom_clock_dividers));
  877. if (!amdgpu_atom_parse_cmd_header(adev->mode_info.atom_context, index, &frev, &crev))
  878. return -EINVAL;
  879. switch (crev) {
  880. case 2:
  881. case 3:
  882. case 5:
  883. /* r6xx, r7xx, evergreen, ni, si.
  884. * TODO: add support for asic_type <= CHIP_RV770*/
  885. if (clock_type == COMPUTE_ENGINE_PLL_PARAM) {
  886. args.v3.ulClockParams = cpu_to_le32((clock_type << 24) | clock);
  887. amdgpu_atom_execute_table(adev->mode_info.atom_context, index, (uint32_t *)&args);
  888. dividers->post_div = args.v3.ucPostDiv;
  889. dividers->enable_post_div = (args.v3.ucCntlFlag &
  890. ATOM_PLL_CNTL_FLAG_PLL_POST_DIV_EN) ? true : false;
  891. dividers->enable_dithen = (args.v3.ucCntlFlag &
  892. ATOM_PLL_CNTL_FLAG_FRACTION_DISABLE) ? false : true;
  893. dividers->whole_fb_div = le16_to_cpu(args.v3.ulFbDiv.usFbDiv);
  894. dividers->frac_fb_div = le16_to_cpu(args.v3.ulFbDiv.usFbDivFrac);
  895. dividers->ref_div = args.v3.ucRefDiv;
  896. dividers->vco_mode = (args.v3.ucCntlFlag &
  897. ATOM_PLL_CNTL_FLAG_MPLL_VCO_MODE) ? 1 : 0;
  898. } else {
  899. /* for SI we use ComputeMemoryClockParam for memory plls */
  900. if (adev->asic_type >= CHIP_TAHITI)
  901. return -EINVAL;
  902. args.v5.ulClockParams = cpu_to_le32((clock_type << 24) | clock);
  903. if (strobe_mode)
  904. args.v5.ucInputFlag = ATOM_PLL_INPUT_FLAG_PLL_STROBE_MODE_EN;
  905. amdgpu_atom_execute_table(adev->mode_info.atom_context, index, (uint32_t *)&args);
  906. dividers->post_div = args.v5.ucPostDiv;
  907. dividers->enable_post_div = (args.v5.ucCntlFlag &
  908. ATOM_PLL_CNTL_FLAG_PLL_POST_DIV_EN) ? true : false;
  909. dividers->enable_dithen = (args.v5.ucCntlFlag &
  910. ATOM_PLL_CNTL_FLAG_FRACTION_DISABLE) ? false : true;
  911. dividers->whole_fb_div = le16_to_cpu(args.v5.ulFbDiv.usFbDiv);
  912. dividers->frac_fb_div = le16_to_cpu(args.v5.ulFbDiv.usFbDivFrac);
  913. dividers->ref_div = args.v5.ucRefDiv;
  914. dividers->vco_mode = (args.v5.ucCntlFlag &
  915. ATOM_PLL_CNTL_FLAG_MPLL_VCO_MODE) ? 1 : 0;
  916. }
  917. break;
  918. case 4:
  919. /* fusion */
  920. args.v4.ulClock = cpu_to_le32(clock); /* 10 khz */
  921. amdgpu_atom_execute_table(adev->mode_info.atom_context, index, (uint32_t *)&args);
  922. dividers->post_divider = dividers->post_div = args.v4.ucPostDiv;
  923. dividers->real_clock = le32_to_cpu(args.v4.ulClock);
  924. break;
  925. case 6:
  926. /* CI */
  927. /* COMPUTE_GPUCLK_INPUT_FLAG_DEFAULT_GPUCLK, COMPUTE_GPUCLK_INPUT_FLAG_SCLK */
  928. args.v6_in.ulClock.ulComputeClockFlag = clock_type;
  929. args.v6_in.ulClock.ulClockFreq = cpu_to_le32(clock); /* 10 khz */
  930. amdgpu_atom_execute_table(adev->mode_info.atom_context, index, (uint32_t *)&args);
  931. dividers->whole_fb_div = le16_to_cpu(args.v6_out.ulFbDiv.usFbDiv);
  932. dividers->frac_fb_div = le16_to_cpu(args.v6_out.ulFbDiv.usFbDivFrac);
  933. dividers->ref_div = args.v6_out.ucPllRefDiv;
  934. dividers->post_div = args.v6_out.ucPllPostDiv;
  935. dividers->flags = args.v6_out.ucPllCntlFlag;
  936. dividers->real_clock = le32_to_cpu(args.v6_out.ulClock.ulClock);
  937. dividers->post_divider = args.v6_out.ulClock.ucPostDiv;
  938. break;
  939. default:
  940. return -EINVAL;
  941. }
  942. return 0;
  943. }
  944. int amdgpu_atombios_get_memory_pll_dividers(struct amdgpu_device *adev,
  945. u32 clock,
  946. bool strobe_mode,
  947. struct atom_mpll_param *mpll_param)
  948. {
  949. COMPUTE_MEMORY_CLOCK_PARAM_PARAMETERS_V2_1 args;
  950. int index = GetIndexIntoMasterTable(COMMAND, ComputeMemoryClockParam);
  951. u8 frev, crev;
  952. memset(&args, 0, sizeof(args));
  953. memset(mpll_param, 0, sizeof(struct atom_mpll_param));
  954. if (!amdgpu_atom_parse_cmd_header(adev->mode_info.atom_context, index, &frev, &crev))
  955. return -EINVAL;
  956. switch (frev) {
  957. case 2:
  958. switch (crev) {
  959. case 1:
  960. /* SI */
  961. args.ulClock = cpu_to_le32(clock); /* 10 khz */
  962. args.ucInputFlag = 0;
  963. if (strobe_mode)
  964. args.ucInputFlag |= MPLL_INPUT_FLAG_STROBE_MODE_EN;
  965. amdgpu_atom_execute_table(adev->mode_info.atom_context, index, (uint32_t *)&args);
  966. mpll_param->clkfrac = le16_to_cpu(args.ulFbDiv.usFbDivFrac);
  967. mpll_param->clkf = le16_to_cpu(args.ulFbDiv.usFbDiv);
  968. mpll_param->post_div = args.ucPostDiv;
  969. mpll_param->dll_speed = args.ucDllSpeed;
  970. mpll_param->bwcntl = args.ucBWCntl;
  971. mpll_param->vco_mode =
  972. (args.ucPllCntlFlag & MPLL_CNTL_FLAG_VCO_MODE_MASK);
  973. mpll_param->yclk_sel =
  974. (args.ucPllCntlFlag & MPLL_CNTL_FLAG_BYPASS_DQ_PLL) ? 1 : 0;
  975. mpll_param->qdr =
  976. (args.ucPllCntlFlag & MPLL_CNTL_FLAG_QDR_ENABLE) ? 1 : 0;
  977. mpll_param->half_rate =
  978. (args.ucPllCntlFlag & MPLL_CNTL_FLAG_AD_HALF_RATE) ? 1 : 0;
  979. break;
  980. default:
  981. return -EINVAL;
  982. }
  983. break;
  984. default:
  985. return -EINVAL;
  986. }
  987. return 0;
  988. }
  989. uint32_t amdgpu_atombios_get_engine_clock(struct amdgpu_device *adev)
  990. {
  991. GET_ENGINE_CLOCK_PS_ALLOCATION args;
  992. int index = GetIndexIntoMasterTable(COMMAND, GetEngineClock);
  993. amdgpu_atom_execute_table(adev->mode_info.atom_context, index, (uint32_t *)&args);
  994. return le32_to_cpu(args.ulReturnEngineClock);
  995. }
  996. uint32_t amdgpu_atombios_get_memory_clock(struct amdgpu_device *adev)
  997. {
  998. GET_MEMORY_CLOCK_PS_ALLOCATION args;
  999. int index = GetIndexIntoMasterTable(COMMAND, GetMemoryClock);
  1000. amdgpu_atom_execute_table(adev->mode_info.atom_context, index, (uint32_t *)&args);
  1001. return le32_to_cpu(args.ulReturnMemoryClock);
  1002. }
  1003. void amdgpu_atombios_set_engine_clock(struct amdgpu_device *adev,
  1004. uint32_t eng_clock)
  1005. {
  1006. SET_ENGINE_CLOCK_PS_ALLOCATION args;
  1007. int index = GetIndexIntoMasterTable(COMMAND, SetEngineClock);
  1008. args.ulTargetEngineClock = cpu_to_le32(eng_clock); /* 10 khz */
  1009. amdgpu_atom_execute_table(adev->mode_info.atom_context, index, (uint32_t *)&args);
  1010. }
  1011. void amdgpu_atombios_set_memory_clock(struct amdgpu_device *adev,
  1012. uint32_t mem_clock)
  1013. {
  1014. SET_MEMORY_CLOCK_PS_ALLOCATION args;
  1015. int index = GetIndexIntoMasterTable(COMMAND, SetMemoryClock);
  1016. if (adev->flags & AMD_IS_APU)
  1017. return;
  1018. args.ulTargetMemoryClock = cpu_to_le32(mem_clock); /* 10 khz */
  1019. amdgpu_atom_execute_table(adev->mode_info.atom_context, index, (uint32_t *)&args);
  1020. }
  1021. void amdgpu_atombios_set_engine_dram_timings(struct amdgpu_device *adev,
  1022. u32 eng_clock, u32 mem_clock)
  1023. {
  1024. SET_ENGINE_CLOCK_PS_ALLOCATION args;
  1025. int index = GetIndexIntoMasterTable(COMMAND, DynamicMemorySettings);
  1026. u32 tmp;
  1027. memset(&args, 0, sizeof(args));
  1028. tmp = eng_clock & SET_CLOCK_FREQ_MASK;
  1029. tmp |= (COMPUTE_ENGINE_PLL_PARAM << 24);
  1030. args.ulTargetEngineClock = cpu_to_le32(tmp);
  1031. if (mem_clock)
  1032. args.sReserved.ulClock = cpu_to_le32(mem_clock & SET_CLOCK_FREQ_MASK);
  1033. amdgpu_atom_execute_table(adev->mode_info.atom_context, index, (uint32_t *)&args);
  1034. }
  1035. void amdgpu_atombios_get_default_voltages(struct amdgpu_device *adev,
  1036. u16 *vddc, u16 *vddci, u16 *mvdd)
  1037. {
  1038. struct amdgpu_mode_info *mode_info = &adev->mode_info;
  1039. int index = GetIndexIntoMasterTable(DATA, FirmwareInfo);
  1040. u8 frev, crev;
  1041. u16 data_offset;
  1042. union firmware_info *firmware_info;
  1043. *vddc = 0;
  1044. *vddci = 0;
  1045. *mvdd = 0;
  1046. if (amdgpu_atom_parse_data_header(mode_info->atom_context, index, NULL,
  1047. &frev, &crev, &data_offset)) {
  1048. firmware_info =
  1049. (union firmware_info *)(mode_info->atom_context->bios +
  1050. data_offset);
  1051. *vddc = le16_to_cpu(firmware_info->info_14.usBootUpVDDCVoltage);
  1052. if ((frev == 2) && (crev >= 2)) {
  1053. *vddci = le16_to_cpu(firmware_info->info_22.usBootUpVDDCIVoltage);
  1054. *mvdd = le16_to_cpu(firmware_info->info_22.usBootUpMVDDCVoltage);
  1055. }
  1056. }
  1057. }
  1058. union set_voltage {
  1059. struct _SET_VOLTAGE_PS_ALLOCATION alloc;
  1060. struct _SET_VOLTAGE_PARAMETERS v1;
  1061. struct _SET_VOLTAGE_PARAMETERS_V2 v2;
  1062. struct _SET_VOLTAGE_PARAMETERS_V1_3 v3;
  1063. };
  1064. int amdgpu_atombios_get_max_vddc(struct amdgpu_device *adev, u8 voltage_type,
  1065. u16 voltage_id, u16 *voltage)
  1066. {
  1067. union set_voltage args;
  1068. int index = GetIndexIntoMasterTable(COMMAND, SetVoltage);
  1069. u8 frev, crev;
  1070. if (!amdgpu_atom_parse_cmd_header(adev->mode_info.atom_context, index, &frev, &crev))
  1071. return -EINVAL;
  1072. switch (crev) {
  1073. case 1:
  1074. return -EINVAL;
  1075. case 2:
  1076. args.v2.ucVoltageType = SET_VOLTAGE_GET_MAX_VOLTAGE;
  1077. args.v2.ucVoltageMode = 0;
  1078. args.v2.usVoltageLevel = 0;
  1079. amdgpu_atom_execute_table(adev->mode_info.atom_context, index, (uint32_t *)&args);
  1080. *voltage = le16_to_cpu(args.v2.usVoltageLevel);
  1081. break;
  1082. case 3:
  1083. args.v3.ucVoltageType = voltage_type;
  1084. args.v3.ucVoltageMode = ATOM_GET_VOLTAGE_LEVEL;
  1085. args.v3.usVoltageLevel = cpu_to_le16(voltage_id);
  1086. amdgpu_atom_execute_table(adev->mode_info.atom_context, index, (uint32_t *)&args);
  1087. *voltage = le16_to_cpu(args.v3.usVoltageLevel);
  1088. break;
  1089. default:
  1090. DRM_ERROR("Unknown table version %d, %d\n", frev, crev);
  1091. return -EINVAL;
  1092. }
  1093. return 0;
  1094. }
  1095. int amdgpu_atombios_get_leakage_vddc_based_on_leakage_idx(struct amdgpu_device *adev,
  1096. u16 *voltage,
  1097. u16 leakage_idx)
  1098. {
  1099. return amdgpu_atombios_get_max_vddc(adev, VOLTAGE_TYPE_VDDC, leakage_idx, voltage);
  1100. }
  1101. void amdgpu_atombios_set_voltage(struct amdgpu_device *adev,
  1102. u16 voltage_level,
  1103. u8 voltage_type)
  1104. {
  1105. union set_voltage args;
  1106. int index = GetIndexIntoMasterTable(COMMAND, SetVoltage);
  1107. u8 frev, crev, volt_index = voltage_level;
  1108. if (!amdgpu_atom_parse_cmd_header(adev->mode_info.atom_context, index, &frev, &crev))
  1109. return;
  1110. /* 0xff01 is a flag rather then an actual voltage */
  1111. if (voltage_level == 0xff01)
  1112. return;
  1113. switch (crev) {
  1114. case 1:
  1115. args.v1.ucVoltageType = voltage_type;
  1116. args.v1.ucVoltageMode = SET_ASIC_VOLTAGE_MODE_ALL_SOURCE;
  1117. args.v1.ucVoltageIndex = volt_index;
  1118. break;
  1119. case 2:
  1120. args.v2.ucVoltageType = voltage_type;
  1121. args.v2.ucVoltageMode = SET_ASIC_VOLTAGE_MODE_SET_VOLTAGE;
  1122. args.v2.usVoltageLevel = cpu_to_le16(voltage_level);
  1123. break;
  1124. case 3:
  1125. args.v3.ucVoltageType = voltage_type;
  1126. args.v3.ucVoltageMode = ATOM_SET_VOLTAGE;
  1127. args.v3.usVoltageLevel = cpu_to_le16(voltage_level);
  1128. break;
  1129. default:
  1130. DRM_ERROR("Unknown table version %d, %d\n", frev, crev);
  1131. return;
  1132. }
  1133. amdgpu_atom_execute_table(adev->mode_info.atom_context, index, (uint32_t *)&args);
  1134. }
  1135. int amdgpu_atombios_get_leakage_id_from_vbios(struct amdgpu_device *adev,
  1136. u16 *leakage_id)
  1137. {
  1138. union set_voltage args;
  1139. int index = GetIndexIntoMasterTable(COMMAND, SetVoltage);
  1140. u8 frev, crev;
  1141. if (!amdgpu_atom_parse_cmd_header(adev->mode_info.atom_context, index, &frev, &crev))
  1142. return -EINVAL;
  1143. switch (crev) {
  1144. case 3:
  1145. case 4:
  1146. args.v3.ucVoltageType = 0;
  1147. args.v3.ucVoltageMode = ATOM_GET_LEAKAGE_ID;
  1148. args.v3.usVoltageLevel = 0;
  1149. amdgpu_atom_execute_table(adev->mode_info.atom_context, index, (uint32_t *)&args);
  1150. *leakage_id = le16_to_cpu(args.v3.usVoltageLevel);
  1151. break;
  1152. default:
  1153. DRM_ERROR("Unknown table version %d, %d\n", frev, crev);
  1154. return -EINVAL;
  1155. }
  1156. return 0;
  1157. }
  1158. int amdgpu_atombios_get_leakage_vddc_based_on_leakage_params(struct amdgpu_device *adev,
  1159. u16 *vddc, u16 *vddci,
  1160. u16 virtual_voltage_id,
  1161. u16 vbios_voltage_id)
  1162. {
  1163. int index = GetIndexIntoMasterTable(DATA, ASIC_ProfilingInfo);
  1164. u8 frev, crev;
  1165. u16 data_offset, size;
  1166. int i, j;
  1167. ATOM_ASIC_PROFILING_INFO_V2_1 *profile;
  1168. u16 *leakage_bin, *vddc_id_buf, *vddc_buf, *vddci_id_buf, *vddci_buf;
  1169. *vddc = 0;
  1170. *vddci = 0;
  1171. if (!amdgpu_atom_parse_data_header(adev->mode_info.atom_context, index, &size,
  1172. &frev, &crev, &data_offset))
  1173. return -EINVAL;
  1174. profile = (ATOM_ASIC_PROFILING_INFO_V2_1 *)
  1175. (adev->mode_info.atom_context->bios + data_offset);
  1176. switch (frev) {
  1177. case 1:
  1178. return -EINVAL;
  1179. case 2:
  1180. switch (crev) {
  1181. case 1:
  1182. if (size < sizeof(ATOM_ASIC_PROFILING_INFO_V2_1))
  1183. return -EINVAL;
  1184. leakage_bin = (u16 *)
  1185. (adev->mode_info.atom_context->bios + data_offset +
  1186. le16_to_cpu(profile->usLeakageBinArrayOffset));
  1187. vddc_id_buf = (u16 *)
  1188. (adev->mode_info.atom_context->bios + data_offset +
  1189. le16_to_cpu(profile->usElbVDDC_IdArrayOffset));
  1190. vddc_buf = (u16 *)
  1191. (adev->mode_info.atom_context->bios + data_offset +
  1192. le16_to_cpu(profile->usElbVDDC_LevelArrayOffset));
  1193. vddci_id_buf = (u16 *)
  1194. (adev->mode_info.atom_context->bios + data_offset +
  1195. le16_to_cpu(profile->usElbVDDCI_IdArrayOffset));
  1196. vddci_buf = (u16 *)
  1197. (adev->mode_info.atom_context->bios + data_offset +
  1198. le16_to_cpu(profile->usElbVDDCI_LevelArrayOffset));
  1199. if (profile->ucElbVDDC_Num > 0) {
  1200. for (i = 0; i < profile->ucElbVDDC_Num; i++) {
  1201. if (vddc_id_buf[i] == virtual_voltage_id) {
  1202. for (j = 0; j < profile->ucLeakageBinNum; j++) {
  1203. if (vbios_voltage_id <= leakage_bin[j]) {
  1204. *vddc = vddc_buf[j * profile->ucElbVDDC_Num + i];
  1205. break;
  1206. }
  1207. }
  1208. break;
  1209. }
  1210. }
  1211. }
  1212. if (profile->ucElbVDDCI_Num > 0) {
  1213. for (i = 0; i < profile->ucElbVDDCI_Num; i++) {
  1214. if (vddci_id_buf[i] == virtual_voltage_id) {
  1215. for (j = 0; j < profile->ucLeakageBinNum; j++) {
  1216. if (vbios_voltage_id <= leakage_bin[j]) {
  1217. *vddci = vddci_buf[j * profile->ucElbVDDCI_Num + i];
  1218. break;
  1219. }
  1220. }
  1221. break;
  1222. }
  1223. }
  1224. }
  1225. break;
  1226. default:
  1227. DRM_ERROR("Unknown table version %d, %d\n", frev, crev);
  1228. return -EINVAL;
  1229. }
  1230. break;
  1231. default:
  1232. DRM_ERROR("Unknown table version %d, %d\n", frev, crev);
  1233. return -EINVAL;
  1234. }
  1235. return 0;
  1236. }
  1237. union get_voltage_info {
  1238. struct _GET_VOLTAGE_INFO_INPUT_PARAMETER_V1_2 in;
  1239. struct _GET_EVV_VOLTAGE_INFO_OUTPUT_PARAMETER_V1_2 evv_out;
  1240. };
  1241. int amdgpu_atombios_get_voltage_evv(struct amdgpu_device *adev,
  1242. u16 virtual_voltage_id,
  1243. u16 *voltage)
  1244. {
  1245. int index = GetIndexIntoMasterTable(COMMAND, GetVoltageInfo);
  1246. u32 entry_id;
  1247. u32 count = adev->pm.dpm.dyn_state.vddc_dependency_on_sclk.count;
  1248. union get_voltage_info args;
  1249. for (entry_id = 0; entry_id < count; entry_id++) {
  1250. if (adev->pm.dpm.dyn_state.vddc_dependency_on_sclk.entries[entry_id].v ==
  1251. virtual_voltage_id)
  1252. break;
  1253. }
  1254. if (entry_id >= count)
  1255. return -EINVAL;
  1256. args.in.ucVoltageType = VOLTAGE_TYPE_VDDC;
  1257. args.in.ucVoltageMode = ATOM_GET_VOLTAGE_EVV_VOLTAGE;
  1258. args.in.usVoltageLevel = cpu_to_le16(virtual_voltage_id);
  1259. args.in.ulSCLKFreq =
  1260. cpu_to_le32(adev->pm.dpm.dyn_state.vddc_dependency_on_sclk.entries[entry_id].clk);
  1261. amdgpu_atom_execute_table(adev->mode_info.atom_context, index, (uint32_t *)&args);
  1262. *voltage = le16_to_cpu(args.evv_out.usVoltageLevel);
  1263. return 0;
  1264. }
  1265. union voltage_object_info {
  1266. struct _ATOM_VOLTAGE_OBJECT_INFO v1;
  1267. struct _ATOM_VOLTAGE_OBJECT_INFO_V2 v2;
  1268. struct _ATOM_VOLTAGE_OBJECT_INFO_V3_1 v3;
  1269. };
  1270. union voltage_object {
  1271. struct _ATOM_VOLTAGE_OBJECT v1;
  1272. struct _ATOM_VOLTAGE_OBJECT_V2 v2;
  1273. union _ATOM_VOLTAGE_OBJECT_V3 v3;
  1274. };
  1275. static ATOM_VOLTAGE_OBJECT_V3 *amdgpu_atombios_lookup_voltage_object_v3(ATOM_VOLTAGE_OBJECT_INFO_V3_1 *v3,
  1276. u8 voltage_type, u8 voltage_mode)
  1277. {
  1278. u32 size = le16_to_cpu(v3->sHeader.usStructureSize);
  1279. u32 offset = offsetof(ATOM_VOLTAGE_OBJECT_INFO_V3_1, asVoltageObj[0]);
  1280. u8 *start = (u8*)v3;
  1281. while (offset < size) {
  1282. ATOM_VOLTAGE_OBJECT_V3 *vo = (ATOM_VOLTAGE_OBJECT_V3 *)(start + offset);
  1283. if ((vo->asGpioVoltageObj.sHeader.ucVoltageType == voltage_type) &&
  1284. (vo->asGpioVoltageObj.sHeader.ucVoltageMode == voltage_mode))
  1285. return vo;
  1286. offset += le16_to_cpu(vo->asGpioVoltageObj.sHeader.usSize);
  1287. }
  1288. return NULL;
  1289. }
  1290. int amdgpu_atombios_get_svi2_info(struct amdgpu_device *adev,
  1291. u8 voltage_type,
  1292. u8 *svd_gpio_id, u8 *svc_gpio_id)
  1293. {
  1294. int index = GetIndexIntoMasterTable(DATA, VoltageObjectInfo);
  1295. u8 frev, crev;
  1296. u16 data_offset, size;
  1297. union voltage_object_info *voltage_info;
  1298. union voltage_object *voltage_object = NULL;
  1299. if (amdgpu_atom_parse_data_header(adev->mode_info.atom_context, index, &size,
  1300. &frev, &crev, &data_offset)) {
  1301. voltage_info = (union voltage_object_info *)
  1302. (adev->mode_info.atom_context->bios + data_offset);
  1303. switch (frev) {
  1304. case 3:
  1305. switch (crev) {
  1306. case 1:
  1307. voltage_object = (union voltage_object *)
  1308. amdgpu_atombios_lookup_voltage_object_v3(&voltage_info->v3,
  1309. voltage_type,
  1310. VOLTAGE_OBJ_SVID2);
  1311. if (voltage_object) {
  1312. *svd_gpio_id = voltage_object->v3.asSVID2Obj.ucSVDGpioId;
  1313. *svc_gpio_id = voltage_object->v3.asSVID2Obj.ucSVCGpioId;
  1314. } else {
  1315. return -EINVAL;
  1316. }
  1317. break;
  1318. default:
  1319. DRM_ERROR("unknown voltage object table\n");
  1320. return -EINVAL;
  1321. }
  1322. break;
  1323. default:
  1324. DRM_ERROR("unknown voltage object table\n");
  1325. return -EINVAL;
  1326. }
  1327. }
  1328. return 0;
  1329. }
  1330. bool
  1331. amdgpu_atombios_is_voltage_gpio(struct amdgpu_device *adev,
  1332. u8 voltage_type, u8 voltage_mode)
  1333. {
  1334. int index = GetIndexIntoMasterTable(DATA, VoltageObjectInfo);
  1335. u8 frev, crev;
  1336. u16 data_offset, size;
  1337. union voltage_object_info *voltage_info;
  1338. if (amdgpu_atom_parse_data_header(adev->mode_info.atom_context, index, &size,
  1339. &frev, &crev, &data_offset)) {
  1340. voltage_info = (union voltage_object_info *)
  1341. (adev->mode_info.atom_context->bios + data_offset);
  1342. switch (frev) {
  1343. case 3:
  1344. switch (crev) {
  1345. case 1:
  1346. if (amdgpu_atombios_lookup_voltage_object_v3(&voltage_info->v3,
  1347. voltage_type, voltage_mode))
  1348. return true;
  1349. break;
  1350. default:
  1351. DRM_ERROR("unknown voltage object table\n");
  1352. return false;
  1353. }
  1354. break;
  1355. default:
  1356. DRM_ERROR("unknown voltage object table\n");
  1357. return false;
  1358. }
  1359. }
  1360. return false;
  1361. }
  1362. int amdgpu_atombios_get_voltage_table(struct amdgpu_device *adev,
  1363. u8 voltage_type, u8 voltage_mode,
  1364. struct atom_voltage_table *voltage_table)
  1365. {
  1366. int index = GetIndexIntoMasterTable(DATA, VoltageObjectInfo);
  1367. u8 frev, crev;
  1368. u16 data_offset, size;
  1369. int i;
  1370. union voltage_object_info *voltage_info;
  1371. union voltage_object *voltage_object = NULL;
  1372. if (amdgpu_atom_parse_data_header(adev->mode_info.atom_context, index, &size,
  1373. &frev, &crev, &data_offset)) {
  1374. voltage_info = (union voltage_object_info *)
  1375. (adev->mode_info.atom_context->bios + data_offset);
  1376. switch (frev) {
  1377. case 3:
  1378. switch (crev) {
  1379. case 1:
  1380. voltage_object = (union voltage_object *)
  1381. amdgpu_atombios_lookup_voltage_object_v3(&voltage_info->v3,
  1382. voltage_type, voltage_mode);
  1383. if (voltage_object) {
  1384. ATOM_GPIO_VOLTAGE_OBJECT_V3 *gpio =
  1385. &voltage_object->v3.asGpioVoltageObj;
  1386. VOLTAGE_LUT_ENTRY_V2 *lut;
  1387. if (gpio->ucGpioEntryNum > MAX_VOLTAGE_ENTRIES)
  1388. return -EINVAL;
  1389. lut = &gpio->asVolGpioLut[0];
  1390. for (i = 0; i < gpio->ucGpioEntryNum; i++) {
  1391. voltage_table->entries[i].value =
  1392. le16_to_cpu(lut->usVoltageValue);
  1393. voltage_table->entries[i].smio_low =
  1394. le32_to_cpu(lut->ulVoltageId);
  1395. lut = (VOLTAGE_LUT_ENTRY_V2 *)
  1396. ((u8 *)lut + sizeof(VOLTAGE_LUT_ENTRY_V2));
  1397. }
  1398. voltage_table->mask_low = le32_to_cpu(gpio->ulGpioMaskVal);
  1399. voltage_table->count = gpio->ucGpioEntryNum;
  1400. voltage_table->phase_delay = gpio->ucPhaseDelay;
  1401. return 0;
  1402. }
  1403. break;
  1404. default:
  1405. DRM_ERROR("unknown voltage object table\n");
  1406. return -EINVAL;
  1407. }
  1408. break;
  1409. default:
  1410. DRM_ERROR("unknown voltage object table\n");
  1411. return -EINVAL;
  1412. }
  1413. }
  1414. return -EINVAL;
  1415. }
  1416. union vram_info {
  1417. struct _ATOM_VRAM_INFO_V3 v1_3;
  1418. struct _ATOM_VRAM_INFO_V4 v1_4;
  1419. struct _ATOM_VRAM_INFO_HEADER_V2_1 v2_1;
  1420. };
  1421. #define MEM_ID_MASK 0xff000000
  1422. #define MEM_ID_SHIFT 24
  1423. #define CLOCK_RANGE_MASK 0x00ffffff
  1424. #define CLOCK_RANGE_SHIFT 0
  1425. #define LOW_NIBBLE_MASK 0xf
  1426. #define DATA_EQU_PREV 0
  1427. #define DATA_FROM_TABLE 4
  1428. int amdgpu_atombios_init_mc_reg_table(struct amdgpu_device *adev,
  1429. u8 module_index,
  1430. struct atom_mc_reg_table *reg_table)
  1431. {
  1432. int index = GetIndexIntoMasterTable(DATA, VRAM_Info);
  1433. u8 frev, crev, num_entries, t_mem_id, num_ranges = 0;
  1434. u32 i = 0, j;
  1435. u16 data_offset, size;
  1436. union vram_info *vram_info;
  1437. memset(reg_table, 0, sizeof(struct atom_mc_reg_table));
  1438. if (amdgpu_atom_parse_data_header(adev->mode_info.atom_context, index, &size,
  1439. &frev, &crev, &data_offset)) {
  1440. vram_info = (union vram_info *)
  1441. (adev->mode_info.atom_context->bios + data_offset);
  1442. switch (frev) {
  1443. case 1:
  1444. DRM_ERROR("old table version %d, %d\n", frev, crev);
  1445. return -EINVAL;
  1446. case 2:
  1447. switch (crev) {
  1448. case 1:
  1449. if (module_index < vram_info->v2_1.ucNumOfVRAMModule) {
  1450. ATOM_INIT_REG_BLOCK *reg_block =
  1451. (ATOM_INIT_REG_BLOCK *)
  1452. ((u8 *)vram_info + le16_to_cpu(vram_info->v2_1.usMemClkPatchTblOffset));
  1453. ATOM_MEMORY_SETTING_DATA_BLOCK *reg_data =
  1454. (ATOM_MEMORY_SETTING_DATA_BLOCK *)
  1455. ((u8 *)reg_block + (2 * sizeof(u16)) +
  1456. le16_to_cpu(reg_block->usRegIndexTblSize));
  1457. ATOM_INIT_REG_INDEX_FORMAT *format = &reg_block->asRegIndexBuf[0];
  1458. num_entries = (u8)((le16_to_cpu(reg_block->usRegIndexTblSize)) /
  1459. sizeof(ATOM_INIT_REG_INDEX_FORMAT)) - 1;
  1460. if (num_entries > VBIOS_MC_REGISTER_ARRAY_SIZE)
  1461. return -EINVAL;
  1462. while (i < num_entries) {
  1463. if (format->ucPreRegDataLength & ACCESS_PLACEHOLDER)
  1464. break;
  1465. reg_table->mc_reg_address[i].s1 =
  1466. (u16)(le16_to_cpu(format->usRegIndex));
  1467. reg_table->mc_reg_address[i].pre_reg_data =
  1468. (u8)(format->ucPreRegDataLength);
  1469. i++;
  1470. format = (ATOM_INIT_REG_INDEX_FORMAT *)
  1471. ((u8 *)format + sizeof(ATOM_INIT_REG_INDEX_FORMAT));
  1472. }
  1473. reg_table->last = i;
  1474. while ((le32_to_cpu(*(u32 *)reg_data) != END_OF_REG_DATA_BLOCK) &&
  1475. (num_ranges < VBIOS_MAX_AC_TIMING_ENTRIES)) {
  1476. t_mem_id = (u8)((le32_to_cpu(*(u32 *)reg_data) & MEM_ID_MASK)
  1477. >> MEM_ID_SHIFT);
  1478. if (module_index == t_mem_id) {
  1479. reg_table->mc_reg_table_entry[num_ranges].mclk_max =
  1480. (u32)((le32_to_cpu(*(u32 *)reg_data) & CLOCK_RANGE_MASK)
  1481. >> CLOCK_RANGE_SHIFT);
  1482. for (i = 0, j = 1; i < reg_table->last; i++) {
  1483. if ((reg_table->mc_reg_address[i].pre_reg_data & LOW_NIBBLE_MASK) == DATA_FROM_TABLE) {
  1484. reg_table->mc_reg_table_entry[num_ranges].mc_data[i] =
  1485. (u32)le32_to_cpu(*((u32 *)reg_data + j));
  1486. j++;
  1487. } else if ((reg_table->mc_reg_address[i].pre_reg_data & LOW_NIBBLE_MASK) == DATA_EQU_PREV) {
  1488. reg_table->mc_reg_table_entry[num_ranges].mc_data[i] =
  1489. reg_table->mc_reg_table_entry[num_ranges].mc_data[i - 1];
  1490. }
  1491. }
  1492. num_ranges++;
  1493. }
  1494. reg_data = (ATOM_MEMORY_SETTING_DATA_BLOCK *)
  1495. ((u8 *)reg_data + le16_to_cpu(reg_block->usRegDataBlkSize));
  1496. }
  1497. if (le32_to_cpu(*(u32 *)reg_data) != END_OF_REG_DATA_BLOCK)
  1498. return -EINVAL;
  1499. reg_table->num_entries = num_ranges;
  1500. } else
  1501. return -EINVAL;
  1502. break;
  1503. default:
  1504. DRM_ERROR("Unknown table version %d, %d\n", frev, crev);
  1505. return -EINVAL;
  1506. }
  1507. break;
  1508. default:
  1509. DRM_ERROR("Unknown table version %d, %d\n", frev, crev);
  1510. return -EINVAL;
  1511. }
  1512. return 0;
  1513. }
  1514. return -EINVAL;
  1515. }
  1516. bool amdgpu_atombios_has_gpu_virtualization_table(struct amdgpu_device *adev)
  1517. {
  1518. int index = GetIndexIntoMasterTable(DATA, GPUVirtualizationInfo);
  1519. u8 frev, crev;
  1520. u16 data_offset, size;
  1521. if (amdgpu_atom_parse_data_header(adev->mode_info.atom_context, index, &size,
  1522. &frev, &crev, &data_offset))
  1523. return true;
  1524. return false;
  1525. }
  1526. void amdgpu_atombios_scratch_regs_lock(struct amdgpu_device *adev, bool lock)
  1527. {
  1528. uint32_t bios_6_scratch;
  1529. bios_6_scratch = RREG32(mmBIOS_SCRATCH_6);
  1530. if (lock) {
  1531. bios_6_scratch |= ATOM_S6_CRITICAL_STATE;
  1532. bios_6_scratch &= ~ATOM_S6_ACC_MODE;
  1533. } else {
  1534. bios_6_scratch &= ~ATOM_S6_CRITICAL_STATE;
  1535. bios_6_scratch |= ATOM_S6_ACC_MODE;
  1536. }
  1537. WREG32(mmBIOS_SCRATCH_6, bios_6_scratch);
  1538. }
  1539. void amdgpu_atombios_scratch_regs_init(struct amdgpu_device *adev)
  1540. {
  1541. uint32_t bios_2_scratch, bios_6_scratch;
  1542. bios_2_scratch = RREG32(mmBIOS_SCRATCH_2);
  1543. bios_6_scratch = RREG32(mmBIOS_SCRATCH_6);
  1544. /* let the bios control the backlight */
  1545. bios_2_scratch &= ~ATOM_S2_VRI_BRIGHT_ENABLE;
  1546. /* tell the bios not to handle mode switching */
  1547. bios_6_scratch |= ATOM_S6_ACC_BLOCK_DISPLAY_SWITCH;
  1548. /* clear the vbios dpms state */
  1549. bios_2_scratch &= ~ATOM_S2_DEVICE_DPMS_STATE;
  1550. WREG32(mmBIOS_SCRATCH_2, bios_2_scratch);
  1551. WREG32(mmBIOS_SCRATCH_6, bios_6_scratch);
  1552. }
  1553. void amdgpu_atombios_scratch_regs_save(struct amdgpu_device *adev)
  1554. {
  1555. int i;
  1556. for (i = 0; i < AMDGPU_BIOS_NUM_SCRATCH; i++)
  1557. adev->bios_scratch[i] = RREG32(mmBIOS_SCRATCH_0 + i);
  1558. }
  1559. void amdgpu_atombios_scratch_regs_restore(struct amdgpu_device *adev)
  1560. {
  1561. int i;
  1562. for (i = 0; i < AMDGPU_BIOS_NUM_SCRATCH; i++)
  1563. WREG32(mmBIOS_SCRATCH_0 + i, adev->bios_scratch[i]);
  1564. }
  1565. /* Atom needs data in little endian format
  1566. * so swap as appropriate when copying data to
  1567. * or from atom. Note that atom operates on
  1568. * dw units.
  1569. */
  1570. void amdgpu_atombios_copy_swap(u8 *dst, u8 *src, u8 num_bytes, bool to_le)
  1571. {
  1572. #ifdef __BIG_ENDIAN
  1573. u8 src_tmp[20], dst_tmp[20]; /* used for byteswapping */
  1574. u32 *dst32, *src32;
  1575. int i;
  1576. memcpy(src_tmp, src, num_bytes);
  1577. src32 = (u32 *)src_tmp;
  1578. dst32 = (u32 *)dst_tmp;
  1579. if (to_le) {
  1580. for (i = 0; i < ((num_bytes + 3) / 4); i++)
  1581. dst32[i] = cpu_to_le32(src32[i]);
  1582. memcpy(dst, dst_tmp, num_bytes);
  1583. } else {
  1584. u8 dws = num_bytes & ~3;
  1585. for (i = 0; i < ((num_bytes + 3) / 4); i++)
  1586. dst32[i] = le32_to_cpu(src32[i]);
  1587. memcpy(dst, dst_tmp, dws);
  1588. if (num_bytes % 4) {
  1589. for (i = 0; i < (num_bytes % 4); i++)
  1590. dst[dws+i] = dst_tmp[dws+i];
  1591. }
  1592. }
  1593. #else
  1594. memcpy(dst, src, num_bytes);
  1595. #endif
  1596. }