amdgpu_atombios.c 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845
  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. } else if (adev->clock.default_dispclk <= 60000) {
  610. DRM_INFO("Changing default dispclk from %dMhz to 625Mhz\n",
  611. adev->clock.default_dispclk / 100);
  612. adev->clock.default_dispclk = 62500;
  613. }
  614. adev->clock.dp_extclk =
  615. le16_to_cpu(firmware_info->info_21.usUniphyDPModeExtClkFreq);
  616. adev->clock.current_dispclk = adev->clock.default_dispclk;
  617. adev->clock.max_pixel_clock = le16_to_cpu(firmware_info->info.usMaxPixelClock);
  618. if (adev->clock.max_pixel_clock == 0)
  619. adev->clock.max_pixel_clock = 40000;
  620. /* not technically a clock, but... */
  621. adev->mode_info.firmware_flags =
  622. le16_to_cpu(firmware_info->info.usFirmwareCapability.susAccess);
  623. ret = 0;
  624. }
  625. adev->pm.current_sclk = adev->clock.default_sclk;
  626. adev->pm.current_mclk = adev->clock.default_mclk;
  627. return ret;
  628. }
  629. union gfx_info {
  630. ATOM_GFX_INFO_V2_1 info;
  631. };
  632. int amdgpu_atombios_get_gfx_info(struct amdgpu_device *adev)
  633. {
  634. struct amdgpu_mode_info *mode_info = &adev->mode_info;
  635. int index = GetIndexIntoMasterTable(DATA, GFX_Info);
  636. uint8_t frev, crev;
  637. uint16_t data_offset;
  638. int ret = -EINVAL;
  639. if (amdgpu_atom_parse_data_header(mode_info->atom_context, index, NULL,
  640. &frev, &crev, &data_offset)) {
  641. union gfx_info *gfx_info = (union gfx_info *)
  642. (mode_info->atom_context->bios + data_offset);
  643. adev->gfx.config.max_shader_engines = gfx_info->info.max_shader_engines;
  644. adev->gfx.config.max_tile_pipes = gfx_info->info.max_tile_pipes;
  645. adev->gfx.config.max_cu_per_sh = gfx_info->info.max_cu_per_sh;
  646. adev->gfx.config.max_sh_per_se = gfx_info->info.max_sh_per_se;
  647. adev->gfx.config.max_backends_per_se = gfx_info->info.max_backends_per_se;
  648. adev->gfx.config.max_texture_channel_caches =
  649. gfx_info->info.max_texture_channel_caches;
  650. ret = 0;
  651. }
  652. return ret;
  653. }
  654. union igp_info {
  655. struct _ATOM_INTEGRATED_SYSTEM_INFO info;
  656. struct _ATOM_INTEGRATED_SYSTEM_INFO_V2 info_2;
  657. struct _ATOM_INTEGRATED_SYSTEM_INFO_V6 info_6;
  658. struct _ATOM_INTEGRATED_SYSTEM_INFO_V1_7 info_7;
  659. struct _ATOM_INTEGRATED_SYSTEM_INFO_V1_8 info_8;
  660. struct _ATOM_INTEGRATED_SYSTEM_INFO_V1_9 info_9;
  661. };
  662. /*
  663. * Return vram width from integrated system info table, if available,
  664. * or 0 if not.
  665. */
  666. int amdgpu_atombios_get_vram_width(struct amdgpu_device *adev)
  667. {
  668. struct amdgpu_mode_info *mode_info = &adev->mode_info;
  669. int index = GetIndexIntoMasterTable(DATA, IntegratedSystemInfo);
  670. u16 data_offset, size;
  671. union igp_info *igp_info;
  672. u8 frev, crev;
  673. /* get any igp specific overrides */
  674. if (amdgpu_atom_parse_data_header(mode_info->atom_context, index, &size,
  675. &frev, &crev, &data_offset)) {
  676. igp_info = (union igp_info *)
  677. (mode_info->atom_context->bios + data_offset);
  678. switch (crev) {
  679. case 8:
  680. case 9:
  681. return igp_info->info_8.ucUMAChannelNumber * 64;
  682. default:
  683. return 0;
  684. }
  685. }
  686. return 0;
  687. }
  688. static void amdgpu_atombios_get_igp_ss_overrides(struct amdgpu_device *adev,
  689. struct amdgpu_atom_ss *ss,
  690. int id)
  691. {
  692. struct amdgpu_mode_info *mode_info = &adev->mode_info;
  693. int index = GetIndexIntoMasterTable(DATA, IntegratedSystemInfo);
  694. u16 data_offset, size;
  695. union igp_info *igp_info;
  696. u8 frev, crev;
  697. u16 percentage = 0, rate = 0;
  698. /* get any igp specific overrides */
  699. if (amdgpu_atom_parse_data_header(mode_info->atom_context, index, &size,
  700. &frev, &crev, &data_offset)) {
  701. igp_info = (union igp_info *)
  702. (mode_info->atom_context->bios + data_offset);
  703. switch (crev) {
  704. case 6:
  705. switch (id) {
  706. case ASIC_INTERNAL_SS_ON_TMDS:
  707. percentage = le16_to_cpu(igp_info->info_6.usDVISSPercentage);
  708. rate = le16_to_cpu(igp_info->info_6.usDVISSpreadRateIn10Hz);
  709. break;
  710. case ASIC_INTERNAL_SS_ON_HDMI:
  711. percentage = le16_to_cpu(igp_info->info_6.usHDMISSPercentage);
  712. rate = le16_to_cpu(igp_info->info_6.usHDMISSpreadRateIn10Hz);
  713. break;
  714. case ASIC_INTERNAL_SS_ON_LVDS:
  715. percentage = le16_to_cpu(igp_info->info_6.usLvdsSSPercentage);
  716. rate = le16_to_cpu(igp_info->info_6.usLvdsSSpreadRateIn10Hz);
  717. break;
  718. }
  719. break;
  720. case 7:
  721. switch (id) {
  722. case ASIC_INTERNAL_SS_ON_TMDS:
  723. percentage = le16_to_cpu(igp_info->info_7.usDVISSPercentage);
  724. rate = le16_to_cpu(igp_info->info_7.usDVISSpreadRateIn10Hz);
  725. break;
  726. case ASIC_INTERNAL_SS_ON_HDMI:
  727. percentage = le16_to_cpu(igp_info->info_7.usHDMISSPercentage);
  728. rate = le16_to_cpu(igp_info->info_7.usHDMISSpreadRateIn10Hz);
  729. break;
  730. case ASIC_INTERNAL_SS_ON_LVDS:
  731. percentage = le16_to_cpu(igp_info->info_7.usLvdsSSPercentage);
  732. rate = le16_to_cpu(igp_info->info_7.usLvdsSSpreadRateIn10Hz);
  733. break;
  734. }
  735. break;
  736. case 8:
  737. switch (id) {
  738. case ASIC_INTERNAL_SS_ON_TMDS:
  739. percentage = le16_to_cpu(igp_info->info_8.usDVISSPercentage);
  740. rate = le16_to_cpu(igp_info->info_8.usDVISSpreadRateIn10Hz);
  741. break;
  742. case ASIC_INTERNAL_SS_ON_HDMI:
  743. percentage = le16_to_cpu(igp_info->info_8.usHDMISSPercentage);
  744. rate = le16_to_cpu(igp_info->info_8.usHDMISSpreadRateIn10Hz);
  745. break;
  746. case ASIC_INTERNAL_SS_ON_LVDS:
  747. percentage = le16_to_cpu(igp_info->info_8.usLvdsSSPercentage);
  748. rate = le16_to_cpu(igp_info->info_8.usLvdsSSpreadRateIn10Hz);
  749. break;
  750. }
  751. break;
  752. case 9:
  753. switch (id) {
  754. case ASIC_INTERNAL_SS_ON_TMDS:
  755. percentage = le16_to_cpu(igp_info->info_9.usDVISSPercentage);
  756. rate = le16_to_cpu(igp_info->info_9.usDVISSpreadRateIn10Hz);
  757. break;
  758. case ASIC_INTERNAL_SS_ON_HDMI:
  759. percentage = le16_to_cpu(igp_info->info_9.usHDMISSPercentage);
  760. rate = le16_to_cpu(igp_info->info_9.usHDMISSpreadRateIn10Hz);
  761. break;
  762. case ASIC_INTERNAL_SS_ON_LVDS:
  763. percentage = le16_to_cpu(igp_info->info_9.usLvdsSSPercentage);
  764. rate = le16_to_cpu(igp_info->info_9.usLvdsSSpreadRateIn10Hz);
  765. break;
  766. }
  767. break;
  768. default:
  769. DRM_ERROR("Unsupported IGP table: %d %d\n", frev, crev);
  770. break;
  771. }
  772. if (percentage)
  773. ss->percentage = percentage;
  774. if (rate)
  775. ss->rate = rate;
  776. }
  777. }
  778. union asic_ss_info {
  779. struct _ATOM_ASIC_INTERNAL_SS_INFO info;
  780. struct _ATOM_ASIC_INTERNAL_SS_INFO_V2 info_2;
  781. struct _ATOM_ASIC_INTERNAL_SS_INFO_V3 info_3;
  782. };
  783. union asic_ss_assignment {
  784. struct _ATOM_ASIC_SS_ASSIGNMENT v1;
  785. struct _ATOM_ASIC_SS_ASSIGNMENT_V2 v2;
  786. struct _ATOM_ASIC_SS_ASSIGNMENT_V3 v3;
  787. };
  788. bool amdgpu_atombios_get_asic_ss_info(struct amdgpu_device *adev,
  789. struct amdgpu_atom_ss *ss,
  790. int id, u32 clock)
  791. {
  792. struct amdgpu_mode_info *mode_info = &adev->mode_info;
  793. int index = GetIndexIntoMasterTable(DATA, ASIC_InternalSS_Info);
  794. uint16_t data_offset, size;
  795. union asic_ss_info *ss_info;
  796. union asic_ss_assignment *ss_assign;
  797. uint8_t frev, crev;
  798. int i, num_indices;
  799. if (id == ASIC_INTERNAL_MEMORY_SS) {
  800. if (!(adev->mode_info.firmware_flags & ATOM_BIOS_INFO_MEMORY_CLOCK_SS_SUPPORT))
  801. return false;
  802. }
  803. if (id == ASIC_INTERNAL_ENGINE_SS) {
  804. if (!(adev->mode_info.firmware_flags & ATOM_BIOS_INFO_ENGINE_CLOCK_SS_SUPPORT))
  805. return false;
  806. }
  807. memset(ss, 0, sizeof(struct amdgpu_atom_ss));
  808. if (amdgpu_atom_parse_data_header(mode_info->atom_context, index, &size,
  809. &frev, &crev, &data_offset)) {
  810. ss_info =
  811. (union asic_ss_info *)(mode_info->atom_context->bios + data_offset);
  812. switch (frev) {
  813. case 1:
  814. num_indices = (size - sizeof(ATOM_COMMON_TABLE_HEADER)) /
  815. sizeof(ATOM_ASIC_SS_ASSIGNMENT);
  816. ss_assign = (union asic_ss_assignment *)((u8 *)&ss_info->info.asSpreadSpectrum[0]);
  817. for (i = 0; i < num_indices; i++) {
  818. if ((ss_assign->v1.ucClockIndication == id) &&
  819. (clock <= le32_to_cpu(ss_assign->v1.ulTargetClockRange))) {
  820. ss->percentage =
  821. le16_to_cpu(ss_assign->v1.usSpreadSpectrumPercentage);
  822. ss->type = ss_assign->v1.ucSpreadSpectrumMode;
  823. ss->rate = le16_to_cpu(ss_assign->v1.usSpreadRateInKhz);
  824. ss->percentage_divider = 100;
  825. return true;
  826. }
  827. ss_assign = (union asic_ss_assignment *)
  828. ((u8 *)ss_assign + sizeof(ATOM_ASIC_SS_ASSIGNMENT));
  829. }
  830. break;
  831. case 2:
  832. num_indices = (size - sizeof(ATOM_COMMON_TABLE_HEADER)) /
  833. sizeof(ATOM_ASIC_SS_ASSIGNMENT_V2);
  834. ss_assign = (union asic_ss_assignment *)((u8 *)&ss_info->info_2.asSpreadSpectrum[0]);
  835. for (i = 0; i < num_indices; i++) {
  836. if ((ss_assign->v2.ucClockIndication == id) &&
  837. (clock <= le32_to_cpu(ss_assign->v2.ulTargetClockRange))) {
  838. ss->percentage =
  839. le16_to_cpu(ss_assign->v2.usSpreadSpectrumPercentage);
  840. ss->type = ss_assign->v2.ucSpreadSpectrumMode;
  841. ss->rate = le16_to_cpu(ss_assign->v2.usSpreadRateIn10Hz);
  842. ss->percentage_divider = 100;
  843. if ((crev == 2) &&
  844. ((id == ASIC_INTERNAL_ENGINE_SS) ||
  845. (id == ASIC_INTERNAL_MEMORY_SS)))
  846. ss->rate /= 100;
  847. return true;
  848. }
  849. ss_assign = (union asic_ss_assignment *)
  850. ((u8 *)ss_assign + sizeof(ATOM_ASIC_SS_ASSIGNMENT_V2));
  851. }
  852. break;
  853. case 3:
  854. num_indices = (size - sizeof(ATOM_COMMON_TABLE_HEADER)) /
  855. sizeof(ATOM_ASIC_SS_ASSIGNMENT_V3);
  856. ss_assign = (union asic_ss_assignment *)((u8 *)&ss_info->info_3.asSpreadSpectrum[0]);
  857. for (i = 0; i < num_indices; i++) {
  858. if ((ss_assign->v3.ucClockIndication == id) &&
  859. (clock <= le32_to_cpu(ss_assign->v3.ulTargetClockRange))) {
  860. ss->percentage =
  861. le16_to_cpu(ss_assign->v3.usSpreadSpectrumPercentage);
  862. ss->type = ss_assign->v3.ucSpreadSpectrumMode;
  863. ss->rate = le16_to_cpu(ss_assign->v3.usSpreadRateIn10Hz);
  864. if (ss_assign->v3.ucSpreadSpectrumMode &
  865. SS_MODE_V3_PERCENTAGE_DIV_BY_1000_MASK)
  866. ss->percentage_divider = 1000;
  867. else
  868. ss->percentage_divider = 100;
  869. if ((id == ASIC_INTERNAL_ENGINE_SS) ||
  870. (id == ASIC_INTERNAL_MEMORY_SS))
  871. ss->rate /= 100;
  872. if (adev->flags & AMD_IS_APU)
  873. amdgpu_atombios_get_igp_ss_overrides(adev, ss, id);
  874. return true;
  875. }
  876. ss_assign = (union asic_ss_assignment *)
  877. ((u8 *)ss_assign + sizeof(ATOM_ASIC_SS_ASSIGNMENT_V3));
  878. }
  879. break;
  880. default:
  881. DRM_ERROR("Unsupported ASIC_InternalSS_Info table: %d %d\n", frev, crev);
  882. break;
  883. }
  884. }
  885. return false;
  886. }
  887. union get_clock_dividers {
  888. struct _COMPUTE_MEMORY_ENGINE_PLL_PARAMETERS v1;
  889. struct _COMPUTE_MEMORY_ENGINE_PLL_PARAMETERS_V2 v2;
  890. struct _COMPUTE_MEMORY_ENGINE_PLL_PARAMETERS_V3 v3;
  891. struct _COMPUTE_MEMORY_ENGINE_PLL_PARAMETERS_V4 v4;
  892. struct _COMPUTE_MEMORY_ENGINE_PLL_PARAMETERS_V5 v5;
  893. struct _COMPUTE_GPU_CLOCK_INPUT_PARAMETERS_V1_6 v6_in;
  894. struct _COMPUTE_GPU_CLOCK_OUTPUT_PARAMETERS_V1_6 v6_out;
  895. };
  896. int amdgpu_atombios_get_clock_dividers(struct amdgpu_device *adev,
  897. u8 clock_type,
  898. u32 clock,
  899. bool strobe_mode,
  900. struct atom_clock_dividers *dividers)
  901. {
  902. union get_clock_dividers args;
  903. int index = GetIndexIntoMasterTable(COMMAND, ComputeMemoryEnginePLL);
  904. u8 frev, crev;
  905. memset(&args, 0, sizeof(args));
  906. memset(dividers, 0, sizeof(struct atom_clock_dividers));
  907. if (!amdgpu_atom_parse_cmd_header(adev->mode_info.atom_context, index, &frev, &crev))
  908. return -EINVAL;
  909. switch (crev) {
  910. case 2:
  911. case 3:
  912. case 5:
  913. /* r6xx, r7xx, evergreen, ni, si.
  914. * TODO: add support for asic_type <= CHIP_RV770*/
  915. if (clock_type == COMPUTE_ENGINE_PLL_PARAM) {
  916. args.v3.ulClockParams = cpu_to_le32((clock_type << 24) | clock);
  917. amdgpu_atom_execute_table(adev->mode_info.atom_context, index, (uint32_t *)&args);
  918. dividers->post_div = args.v3.ucPostDiv;
  919. dividers->enable_post_div = (args.v3.ucCntlFlag &
  920. ATOM_PLL_CNTL_FLAG_PLL_POST_DIV_EN) ? true : false;
  921. dividers->enable_dithen = (args.v3.ucCntlFlag &
  922. ATOM_PLL_CNTL_FLAG_FRACTION_DISABLE) ? false : true;
  923. dividers->whole_fb_div = le16_to_cpu(args.v3.ulFbDiv.usFbDiv);
  924. dividers->frac_fb_div = le16_to_cpu(args.v3.ulFbDiv.usFbDivFrac);
  925. dividers->ref_div = args.v3.ucRefDiv;
  926. dividers->vco_mode = (args.v3.ucCntlFlag &
  927. ATOM_PLL_CNTL_FLAG_MPLL_VCO_MODE) ? 1 : 0;
  928. } else {
  929. /* for SI we use ComputeMemoryClockParam for memory plls */
  930. if (adev->asic_type >= CHIP_TAHITI)
  931. return -EINVAL;
  932. args.v5.ulClockParams = cpu_to_le32((clock_type << 24) | clock);
  933. if (strobe_mode)
  934. args.v5.ucInputFlag = ATOM_PLL_INPUT_FLAG_PLL_STROBE_MODE_EN;
  935. amdgpu_atom_execute_table(adev->mode_info.atom_context, index, (uint32_t *)&args);
  936. dividers->post_div = args.v5.ucPostDiv;
  937. dividers->enable_post_div = (args.v5.ucCntlFlag &
  938. ATOM_PLL_CNTL_FLAG_PLL_POST_DIV_EN) ? true : false;
  939. dividers->enable_dithen = (args.v5.ucCntlFlag &
  940. ATOM_PLL_CNTL_FLAG_FRACTION_DISABLE) ? false : true;
  941. dividers->whole_fb_div = le16_to_cpu(args.v5.ulFbDiv.usFbDiv);
  942. dividers->frac_fb_div = le16_to_cpu(args.v5.ulFbDiv.usFbDivFrac);
  943. dividers->ref_div = args.v5.ucRefDiv;
  944. dividers->vco_mode = (args.v5.ucCntlFlag &
  945. ATOM_PLL_CNTL_FLAG_MPLL_VCO_MODE) ? 1 : 0;
  946. }
  947. break;
  948. case 4:
  949. /* fusion */
  950. args.v4.ulClock = cpu_to_le32(clock); /* 10 khz */
  951. amdgpu_atom_execute_table(adev->mode_info.atom_context, index, (uint32_t *)&args);
  952. dividers->post_divider = dividers->post_div = args.v4.ucPostDiv;
  953. dividers->real_clock = le32_to_cpu(args.v4.ulClock);
  954. break;
  955. case 6:
  956. /* CI */
  957. /* COMPUTE_GPUCLK_INPUT_FLAG_DEFAULT_GPUCLK, COMPUTE_GPUCLK_INPUT_FLAG_SCLK */
  958. args.v6_in.ulClock.ulComputeClockFlag = clock_type;
  959. args.v6_in.ulClock.ulClockFreq = cpu_to_le32(clock); /* 10 khz */
  960. amdgpu_atom_execute_table(adev->mode_info.atom_context, index, (uint32_t *)&args);
  961. dividers->whole_fb_div = le16_to_cpu(args.v6_out.ulFbDiv.usFbDiv);
  962. dividers->frac_fb_div = le16_to_cpu(args.v6_out.ulFbDiv.usFbDivFrac);
  963. dividers->ref_div = args.v6_out.ucPllRefDiv;
  964. dividers->post_div = args.v6_out.ucPllPostDiv;
  965. dividers->flags = args.v6_out.ucPllCntlFlag;
  966. dividers->real_clock = le32_to_cpu(args.v6_out.ulClock.ulClock);
  967. dividers->post_divider = args.v6_out.ulClock.ucPostDiv;
  968. break;
  969. default:
  970. return -EINVAL;
  971. }
  972. return 0;
  973. }
  974. int amdgpu_atombios_get_memory_pll_dividers(struct amdgpu_device *adev,
  975. u32 clock,
  976. bool strobe_mode,
  977. struct atom_mpll_param *mpll_param)
  978. {
  979. COMPUTE_MEMORY_CLOCK_PARAM_PARAMETERS_V2_1 args;
  980. int index = GetIndexIntoMasterTable(COMMAND, ComputeMemoryClockParam);
  981. u8 frev, crev;
  982. memset(&args, 0, sizeof(args));
  983. memset(mpll_param, 0, sizeof(struct atom_mpll_param));
  984. if (!amdgpu_atom_parse_cmd_header(adev->mode_info.atom_context, index, &frev, &crev))
  985. return -EINVAL;
  986. switch (frev) {
  987. case 2:
  988. switch (crev) {
  989. case 1:
  990. /* SI */
  991. args.ulClock = cpu_to_le32(clock); /* 10 khz */
  992. args.ucInputFlag = 0;
  993. if (strobe_mode)
  994. args.ucInputFlag |= MPLL_INPUT_FLAG_STROBE_MODE_EN;
  995. amdgpu_atom_execute_table(adev->mode_info.atom_context, index, (uint32_t *)&args);
  996. mpll_param->clkfrac = le16_to_cpu(args.ulFbDiv.usFbDivFrac);
  997. mpll_param->clkf = le16_to_cpu(args.ulFbDiv.usFbDiv);
  998. mpll_param->post_div = args.ucPostDiv;
  999. mpll_param->dll_speed = args.ucDllSpeed;
  1000. mpll_param->bwcntl = args.ucBWCntl;
  1001. mpll_param->vco_mode =
  1002. (args.ucPllCntlFlag & MPLL_CNTL_FLAG_VCO_MODE_MASK);
  1003. mpll_param->yclk_sel =
  1004. (args.ucPllCntlFlag & MPLL_CNTL_FLAG_BYPASS_DQ_PLL) ? 1 : 0;
  1005. mpll_param->qdr =
  1006. (args.ucPllCntlFlag & MPLL_CNTL_FLAG_QDR_ENABLE) ? 1 : 0;
  1007. mpll_param->half_rate =
  1008. (args.ucPllCntlFlag & MPLL_CNTL_FLAG_AD_HALF_RATE) ? 1 : 0;
  1009. break;
  1010. default:
  1011. return -EINVAL;
  1012. }
  1013. break;
  1014. default:
  1015. return -EINVAL;
  1016. }
  1017. return 0;
  1018. }
  1019. void amdgpu_atombios_set_engine_dram_timings(struct amdgpu_device *adev,
  1020. u32 eng_clock, u32 mem_clock)
  1021. {
  1022. SET_ENGINE_CLOCK_PS_ALLOCATION args;
  1023. int index = GetIndexIntoMasterTable(COMMAND, DynamicMemorySettings);
  1024. u32 tmp;
  1025. memset(&args, 0, sizeof(args));
  1026. tmp = eng_clock & SET_CLOCK_FREQ_MASK;
  1027. tmp |= (COMPUTE_ENGINE_PLL_PARAM << 24);
  1028. args.ulTargetEngineClock = cpu_to_le32(tmp);
  1029. if (mem_clock)
  1030. args.sReserved.ulClock = cpu_to_le32(mem_clock & SET_CLOCK_FREQ_MASK);
  1031. amdgpu_atom_execute_table(adev->mode_info.atom_context, index, (uint32_t *)&args);
  1032. }
  1033. void amdgpu_atombios_get_default_voltages(struct amdgpu_device *adev,
  1034. u16 *vddc, u16 *vddci, u16 *mvdd)
  1035. {
  1036. struct amdgpu_mode_info *mode_info = &adev->mode_info;
  1037. int index = GetIndexIntoMasterTable(DATA, FirmwareInfo);
  1038. u8 frev, crev;
  1039. u16 data_offset;
  1040. union firmware_info *firmware_info;
  1041. *vddc = 0;
  1042. *vddci = 0;
  1043. *mvdd = 0;
  1044. if (amdgpu_atom_parse_data_header(mode_info->atom_context, index, NULL,
  1045. &frev, &crev, &data_offset)) {
  1046. firmware_info =
  1047. (union firmware_info *)(mode_info->atom_context->bios +
  1048. data_offset);
  1049. *vddc = le16_to_cpu(firmware_info->info_14.usBootUpVDDCVoltage);
  1050. if ((frev == 2) && (crev >= 2)) {
  1051. *vddci = le16_to_cpu(firmware_info->info_22.usBootUpVDDCIVoltage);
  1052. *mvdd = le16_to_cpu(firmware_info->info_22.usBootUpMVDDCVoltage);
  1053. }
  1054. }
  1055. }
  1056. union set_voltage {
  1057. struct _SET_VOLTAGE_PS_ALLOCATION alloc;
  1058. struct _SET_VOLTAGE_PARAMETERS v1;
  1059. struct _SET_VOLTAGE_PARAMETERS_V2 v2;
  1060. struct _SET_VOLTAGE_PARAMETERS_V1_3 v3;
  1061. };
  1062. int amdgpu_atombios_get_max_vddc(struct amdgpu_device *adev, u8 voltage_type,
  1063. u16 voltage_id, u16 *voltage)
  1064. {
  1065. union set_voltage args;
  1066. int index = GetIndexIntoMasterTable(COMMAND, SetVoltage);
  1067. u8 frev, crev;
  1068. if (!amdgpu_atom_parse_cmd_header(adev->mode_info.atom_context, index, &frev, &crev))
  1069. return -EINVAL;
  1070. switch (crev) {
  1071. case 1:
  1072. return -EINVAL;
  1073. case 2:
  1074. args.v2.ucVoltageType = SET_VOLTAGE_GET_MAX_VOLTAGE;
  1075. args.v2.ucVoltageMode = 0;
  1076. args.v2.usVoltageLevel = 0;
  1077. amdgpu_atom_execute_table(adev->mode_info.atom_context, index, (uint32_t *)&args);
  1078. *voltage = le16_to_cpu(args.v2.usVoltageLevel);
  1079. break;
  1080. case 3:
  1081. args.v3.ucVoltageType = voltage_type;
  1082. args.v3.ucVoltageMode = ATOM_GET_VOLTAGE_LEVEL;
  1083. args.v3.usVoltageLevel = cpu_to_le16(voltage_id);
  1084. amdgpu_atom_execute_table(adev->mode_info.atom_context, index, (uint32_t *)&args);
  1085. *voltage = le16_to_cpu(args.v3.usVoltageLevel);
  1086. break;
  1087. default:
  1088. DRM_ERROR("Unknown table version %d, %d\n", frev, crev);
  1089. return -EINVAL;
  1090. }
  1091. return 0;
  1092. }
  1093. int amdgpu_atombios_get_leakage_vddc_based_on_leakage_idx(struct amdgpu_device *adev,
  1094. u16 *voltage,
  1095. u16 leakage_idx)
  1096. {
  1097. return amdgpu_atombios_get_max_vddc(adev, VOLTAGE_TYPE_VDDC, leakage_idx, voltage);
  1098. }
  1099. int amdgpu_atombios_get_leakage_id_from_vbios(struct amdgpu_device *adev,
  1100. u16 *leakage_id)
  1101. {
  1102. union set_voltage args;
  1103. int index = GetIndexIntoMasterTable(COMMAND, SetVoltage);
  1104. u8 frev, crev;
  1105. if (!amdgpu_atom_parse_cmd_header(adev->mode_info.atom_context, index, &frev, &crev))
  1106. return -EINVAL;
  1107. switch (crev) {
  1108. case 3:
  1109. case 4:
  1110. args.v3.ucVoltageType = 0;
  1111. args.v3.ucVoltageMode = ATOM_GET_LEAKAGE_ID;
  1112. args.v3.usVoltageLevel = 0;
  1113. amdgpu_atom_execute_table(adev->mode_info.atom_context, index, (uint32_t *)&args);
  1114. *leakage_id = le16_to_cpu(args.v3.usVoltageLevel);
  1115. break;
  1116. default:
  1117. DRM_ERROR("Unknown table version %d, %d\n", frev, crev);
  1118. return -EINVAL;
  1119. }
  1120. return 0;
  1121. }
  1122. int amdgpu_atombios_get_leakage_vddc_based_on_leakage_params(struct amdgpu_device *adev,
  1123. u16 *vddc, u16 *vddci,
  1124. u16 virtual_voltage_id,
  1125. u16 vbios_voltage_id)
  1126. {
  1127. int index = GetIndexIntoMasterTable(DATA, ASIC_ProfilingInfo);
  1128. u8 frev, crev;
  1129. u16 data_offset, size;
  1130. int i, j;
  1131. ATOM_ASIC_PROFILING_INFO_V2_1 *profile;
  1132. u16 *leakage_bin, *vddc_id_buf, *vddc_buf, *vddci_id_buf, *vddci_buf;
  1133. *vddc = 0;
  1134. *vddci = 0;
  1135. if (!amdgpu_atom_parse_data_header(adev->mode_info.atom_context, index, &size,
  1136. &frev, &crev, &data_offset))
  1137. return -EINVAL;
  1138. profile = (ATOM_ASIC_PROFILING_INFO_V2_1 *)
  1139. (adev->mode_info.atom_context->bios + data_offset);
  1140. switch (frev) {
  1141. case 1:
  1142. return -EINVAL;
  1143. case 2:
  1144. switch (crev) {
  1145. case 1:
  1146. if (size < sizeof(ATOM_ASIC_PROFILING_INFO_V2_1))
  1147. return -EINVAL;
  1148. leakage_bin = (u16 *)
  1149. (adev->mode_info.atom_context->bios + data_offset +
  1150. le16_to_cpu(profile->usLeakageBinArrayOffset));
  1151. vddc_id_buf = (u16 *)
  1152. (adev->mode_info.atom_context->bios + data_offset +
  1153. le16_to_cpu(profile->usElbVDDC_IdArrayOffset));
  1154. vddc_buf = (u16 *)
  1155. (adev->mode_info.atom_context->bios + data_offset +
  1156. le16_to_cpu(profile->usElbVDDC_LevelArrayOffset));
  1157. vddci_id_buf = (u16 *)
  1158. (adev->mode_info.atom_context->bios + data_offset +
  1159. le16_to_cpu(profile->usElbVDDCI_IdArrayOffset));
  1160. vddci_buf = (u16 *)
  1161. (adev->mode_info.atom_context->bios + data_offset +
  1162. le16_to_cpu(profile->usElbVDDCI_LevelArrayOffset));
  1163. if (profile->ucElbVDDC_Num > 0) {
  1164. for (i = 0; i < profile->ucElbVDDC_Num; i++) {
  1165. if (vddc_id_buf[i] == virtual_voltage_id) {
  1166. for (j = 0; j < profile->ucLeakageBinNum; j++) {
  1167. if (vbios_voltage_id <= leakage_bin[j]) {
  1168. *vddc = vddc_buf[j * profile->ucElbVDDC_Num + i];
  1169. break;
  1170. }
  1171. }
  1172. break;
  1173. }
  1174. }
  1175. }
  1176. if (profile->ucElbVDDCI_Num > 0) {
  1177. for (i = 0; i < profile->ucElbVDDCI_Num; i++) {
  1178. if (vddci_id_buf[i] == virtual_voltage_id) {
  1179. for (j = 0; j < profile->ucLeakageBinNum; j++) {
  1180. if (vbios_voltage_id <= leakage_bin[j]) {
  1181. *vddci = vddci_buf[j * profile->ucElbVDDCI_Num + i];
  1182. break;
  1183. }
  1184. }
  1185. break;
  1186. }
  1187. }
  1188. }
  1189. break;
  1190. default:
  1191. DRM_ERROR("Unknown table version %d, %d\n", frev, crev);
  1192. return -EINVAL;
  1193. }
  1194. break;
  1195. default:
  1196. DRM_ERROR("Unknown table version %d, %d\n", frev, crev);
  1197. return -EINVAL;
  1198. }
  1199. return 0;
  1200. }
  1201. union get_voltage_info {
  1202. struct _GET_VOLTAGE_INFO_INPUT_PARAMETER_V1_2 in;
  1203. struct _GET_EVV_VOLTAGE_INFO_OUTPUT_PARAMETER_V1_2 evv_out;
  1204. };
  1205. int amdgpu_atombios_get_voltage_evv(struct amdgpu_device *adev,
  1206. u16 virtual_voltage_id,
  1207. u16 *voltage)
  1208. {
  1209. int index = GetIndexIntoMasterTable(COMMAND, GetVoltageInfo);
  1210. u32 entry_id;
  1211. u32 count = adev->pm.dpm.dyn_state.vddc_dependency_on_sclk.count;
  1212. union get_voltage_info args;
  1213. for (entry_id = 0; entry_id < count; entry_id++) {
  1214. if (adev->pm.dpm.dyn_state.vddc_dependency_on_sclk.entries[entry_id].v ==
  1215. virtual_voltage_id)
  1216. break;
  1217. }
  1218. if (entry_id >= count)
  1219. return -EINVAL;
  1220. args.in.ucVoltageType = VOLTAGE_TYPE_VDDC;
  1221. args.in.ucVoltageMode = ATOM_GET_VOLTAGE_EVV_VOLTAGE;
  1222. args.in.usVoltageLevel = cpu_to_le16(virtual_voltage_id);
  1223. args.in.ulSCLKFreq =
  1224. cpu_to_le32(adev->pm.dpm.dyn_state.vddc_dependency_on_sclk.entries[entry_id].clk);
  1225. amdgpu_atom_execute_table(adev->mode_info.atom_context, index, (uint32_t *)&args);
  1226. *voltage = le16_to_cpu(args.evv_out.usVoltageLevel);
  1227. return 0;
  1228. }
  1229. union voltage_object_info {
  1230. struct _ATOM_VOLTAGE_OBJECT_INFO v1;
  1231. struct _ATOM_VOLTAGE_OBJECT_INFO_V2 v2;
  1232. struct _ATOM_VOLTAGE_OBJECT_INFO_V3_1 v3;
  1233. };
  1234. union voltage_object {
  1235. struct _ATOM_VOLTAGE_OBJECT v1;
  1236. struct _ATOM_VOLTAGE_OBJECT_V2 v2;
  1237. union _ATOM_VOLTAGE_OBJECT_V3 v3;
  1238. };
  1239. static ATOM_VOLTAGE_OBJECT_V3 *amdgpu_atombios_lookup_voltage_object_v3(ATOM_VOLTAGE_OBJECT_INFO_V3_1 *v3,
  1240. u8 voltage_type, u8 voltage_mode)
  1241. {
  1242. u32 size = le16_to_cpu(v3->sHeader.usStructureSize);
  1243. u32 offset = offsetof(ATOM_VOLTAGE_OBJECT_INFO_V3_1, asVoltageObj[0]);
  1244. u8 *start = (u8*)v3;
  1245. while (offset < size) {
  1246. ATOM_VOLTAGE_OBJECT_V3 *vo = (ATOM_VOLTAGE_OBJECT_V3 *)(start + offset);
  1247. if ((vo->asGpioVoltageObj.sHeader.ucVoltageType == voltage_type) &&
  1248. (vo->asGpioVoltageObj.sHeader.ucVoltageMode == voltage_mode))
  1249. return vo;
  1250. offset += le16_to_cpu(vo->asGpioVoltageObj.sHeader.usSize);
  1251. }
  1252. return NULL;
  1253. }
  1254. int amdgpu_atombios_get_svi2_info(struct amdgpu_device *adev,
  1255. u8 voltage_type,
  1256. u8 *svd_gpio_id, u8 *svc_gpio_id)
  1257. {
  1258. int index = GetIndexIntoMasterTable(DATA, VoltageObjectInfo);
  1259. u8 frev, crev;
  1260. u16 data_offset, size;
  1261. union voltage_object_info *voltage_info;
  1262. union voltage_object *voltage_object = NULL;
  1263. if (amdgpu_atom_parse_data_header(adev->mode_info.atom_context, index, &size,
  1264. &frev, &crev, &data_offset)) {
  1265. voltage_info = (union voltage_object_info *)
  1266. (adev->mode_info.atom_context->bios + data_offset);
  1267. switch (frev) {
  1268. case 3:
  1269. switch (crev) {
  1270. case 1:
  1271. voltage_object = (union voltage_object *)
  1272. amdgpu_atombios_lookup_voltage_object_v3(&voltage_info->v3,
  1273. voltage_type,
  1274. VOLTAGE_OBJ_SVID2);
  1275. if (voltage_object) {
  1276. *svd_gpio_id = voltage_object->v3.asSVID2Obj.ucSVDGpioId;
  1277. *svc_gpio_id = voltage_object->v3.asSVID2Obj.ucSVCGpioId;
  1278. } else {
  1279. return -EINVAL;
  1280. }
  1281. break;
  1282. default:
  1283. DRM_ERROR("unknown voltage object table\n");
  1284. return -EINVAL;
  1285. }
  1286. break;
  1287. default:
  1288. DRM_ERROR("unknown voltage object table\n");
  1289. return -EINVAL;
  1290. }
  1291. }
  1292. return 0;
  1293. }
  1294. bool
  1295. amdgpu_atombios_is_voltage_gpio(struct amdgpu_device *adev,
  1296. u8 voltage_type, u8 voltage_mode)
  1297. {
  1298. int index = GetIndexIntoMasterTable(DATA, VoltageObjectInfo);
  1299. u8 frev, crev;
  1300. u16 data_offset, size;
  1301. union voltage_object_info *voltage_info;
  1302. if (amdgpu_atom_parse_data_header(adev->mode_info.atom_context, index, &size,
  1303. &frev, &crev, &data_offset)) {
  1304. voltage_info = (union voltage_object_info *)
  1305. (adev->mode_info.atom_context->bios + data_offset);
  1306. switch (frev) {
  1307. case 3:
  1308. switch (crev) {
  1309. case 1:
  1310. if (amdgpu_atombios_lookup_voltage_object_v3(&voltage_info->v3,
  1311. voltage_type, voltage_mode))
  1312. return true;
  1313. break;
  1314. default:
  1315. DRM_ERROR("unknown voltage object table\n");
  1316. return false;
  1317. }
  1318. break;
  1319. default:
  1320. DRM_ERROR("unknown voltage object table\n");
  1321. return false;
  1322. }
  1323. }
  1324. return false;
  1325. }
  1326. int amdgpu_atombios_get_voltage_table(struct amdgpu_device *adev,
  1327. u8 voltage_type, u8 voltage_mode,
  1328. struct atom_voltage_table *voltage_table)
  1329. {
  1330. int index = GetIndexIntoMasterTable(DATA, VoltageObjectInfo);
  1331. u8 frev, crev;
  1332. u16 data_offset, size;
  1333. int i;
  1334. union voltage_object_info *voltage_info;
  1335. union voltage_object *voltage_object = NULL;
  1336. if (amdgpu_atom_parse_data_header(adev->mode_info.atom_context, index, &size,
  1337. &frev, &crev, &data_offset)) {
  1338. voltage_info = (union voltage_object_info *)
  1339. (adev->mode_info.atom_context->bios + data_offset);
  1340. switch (frev) {
  1341. case 3:
  1342. switch (crev) {
  1343. case 1:
  1344. voltage_object = (union voltage_object *)
  1345. amdgpu_atombios_lookup_voltage_object_v3(&voltage_info->v3,
  1346. voltage_type, voltage_mode);
  1347. if (voltage_object) {
  1348. ATOM_GPIO_VOLTAGE_OBJECT_V3 *gpio =
  1349. &voltage_object->v3.asGpioVoltageObj;
  1350. VOLTAGE_LUT_ENTRY_V2 *lut;
  1351. if (gpio->ucGpioEntryNum > MAX_VOLTAGE_ENTRIES)
  1352. return -EINVAL;
  1353. lut = &gpio->asVolGpioLut[0];
  1354. for (i = 0; i < gpio->ucGpioEntryNum; i++) {
  1355. voltage_table->entries[i].value =
  1356. le16_to_cpu(lut->usVoltageValue);
  1357. voltage_table->entries[i].smio_low =
  1358. le32_to_cpu(lut->ulVoltageId);
  1359. lut = (VOLTAGE_LUT_ENTRY_V2 *)
  1360. ((u8 *)lut + sizeof(VOLTAGE_LUT_ENTRY_V2));
  1361. }
  1362. voltage_table->mask_low = le32_to_cpu(gpio->ulGpioMaskVal);
  1363. voltage_table->count = gpio->ucGpioEntryNum;
  1364. voltage_table->phase_delay = gpio->ucPhaseDelay;
  1365. return 0;
  1366. }
  1367. break;
  1368. default:
  1369. DRM_ERROR("unknown voltage object table\n");
  1370. return -EINVAL;
  1371. }
  1372. break;
  1373. default:
  1374. DRM_ERROR("unknown voltage object table\n");
  1375. return -EINVAL;
  1376. }
  1377. }
  1378. return -EINVAL;
  1379. }
  1380. union vram_info {
  1381. struct _ATOM_VRAM_INFO_V3 v1_3;
  1382. struct _ATOM_VRAM_INFO_V4 v1_4;
  1383. struct _ATOM_VRAM_INFO_HEADER_V2_1 v2_1;
  1384. };
  1385. #define MEM_ID_MASK 0xff000000
  1386. #define MEM_ID_SHIFT 24
  1387. #define CLOCK_RANGE_MASK 0x00ffffff
  1388. #define CLOCK_RANGE_SHIFT 0
  1389. #define LOW_NIBBLE_MASK 0xf
  1390. #define DATA_EQU_PREV 0
  1391. #define DATA_FROM_TABLE 4
  1392. int amdgpu_atombios_init_mc_reg_table(struct amdgpu_device *adev,
  1393. u8 module_index,
  1394. struct atom_mc_reg_table *reg_table)
  1395. {
  1396. int index = GetIndexIntoMasterTable(DATA, VRAM_Info);
  1397. u8 frev, crev, num_entries, t_mem_id, num_ranges = 0;
  1398. u32 i = 0, j;
  1399. u16 data_offset, size;
  1400. union vram_info *vram_info;
  1401. memset(reg_table, 0, sizeof(struct atom_mc_reg_table));
  1402. if (amdgpu_atom_parse_data_header(adev->mode_info.atom_context, index, &size,
  1403. &frev, &crev, &data_offset)) {
  1404. vram_info = (union vram_info *)
  1405. (adev->mode_info.atom_context->bios + data_offset);
  1406. switch (frev) {
  1407. case 1:
  1408. DRM_ERROR("old table version %d, %d\n", frev, crev);
  1409. return -EINVAL;
  1410. case 2:
  1411. switch (crev) {
  1412. case 1:
  1413. if (module_index < vram_info->v2_1.ucNumOfVRAMModule) {
  1414. ATOM_INIT_REG_BLOCK *reg_block =
  1415. (ATOM_INIT_REG_BLOCK *)
  1416. ((u8 *)vram_info + le16_to_cpu(vram_info->v2_1.usMemClkPatchTblOffset));
  1417. ATOM_MEMORY_SETTING_DATA_BLOCK *reg_data =
  1418. (ATOM_MEMORY_SETTING_DATA_BLOCK *)
  1419. ((u8 *)reg_block + (2 * sizeof(u16)) +
  1420. le16_to_cpu(reg_block->usRegIndexTblSize));
  1421. ATOM_INIT_REG_INDEX_FORMAT *format = &reg_block->asRegIndexBuf[0];
  1422. num_entries = (u8)((le16_to_cpu(reg_block->usRegIndexTblSize)) /
  1423. sizeof(ATOM_INIT_REG_INDEX_FORMAT)) - 1;
  1424. if (num_entries > VBIOS_MC_REGISTER_ARRAY_SIZE)
  1425. return -EINVAL;
  1426. while (i < num_entries) {
  1427. if (format->ucPreRegDataLength & ACCESS_PLACEHOLDER)
  1428. break;
  1429. reg_table->mc_reg_address[i].s1 =
  1430. (u16)(le16_to_cpu(format->usRegIndex));
  1431. reg_table->mc_reg_address[i].pre_reg_data =
  1432. (u8)(format->ucPreRegDataLength);
  1433. i++;
  1434. format = (ATOM_INIT_REG_INDEX_FORMAT *)
  1435. ((u8 *)format + sizeof(ATOM_INIT_REG_INDEX_FORMAT));
  1436. }
  1437. reg_table->last = i;
  1438. while ((le32_to_cpu(*(u32 *)reg_data) != END_OF_REG_DATA_BLOCK) &&
  1439. (num_ranges < VBIOS_MAX_AC_TIMING_ENTRIES)) {
  1440. t_mem_id = (u8)((le32_to_cpu(*(u32 *)reg_data) & MEM_ID_MASK)
  1441. >> MEM_ID_SHIFT);
  1442. if (module_index == t_mem_id) {
  1443. reg_table->mc_reg_table_entry[num_ranges].mclk_max =
  1444. (u32)((le32_to_cpu(*(u32 *)reg_data) & CLOCK_RANGE_MASK)
  1445. >> CLOCK_RANGE_SHIFT);
  1446. for (i = 0, j = 1; i < reg_table->last; i++) {
  1447. if ((reg_table->mc_reg_address[i].pre_reg_data & LOW_NIBBLE_MASK) == DATA_FROM_TABLE) {
  1448. reg_table->mc_reg_table_entry[num_ranges].mc_data[i] =
  1449. (u32)le32_to_cpu(*((u32 *)reg_data + j));
  1450. j++;
  1451. } else if ((reg_table->mc_reg_address[i].pre_reg_data & LOW_NIBBLE_MASK) == DATA_EQU_PREV) {
  1452. reg_table->mc_reg_table_entry[num_ranges].mc_data[i] =
  1453. reg_table->mc_reg_table_entry[num_ranges].mc_data[i - 1];
  1454. }
  1455. }
  1456. num_ranges++;
  1457. }
  1458. reg_data = (ATOM_MEMORY_SETTING_DATA_BLOCK *)
  1459. ((u8 *)reg_data + le16_to_cpu(reg_block->usRegDataBlkSize));
  1460. }
  1461. if (le32_to_cpu(*(u32 *)reg_data) != END_OF_REG_DATA_BLOCK)
  1462. return -EINVAL;
  1463. reg_table->num_entries = num_ranges;
  1464. } else
  1465. return -EINVAL;
  1466. break;
  1467. default:
  1468. DRM_ERROR("Unknown table version %d, %d\n", frev, crev);
  1469. return -EINVAL;
  1470. }
  1471. break;
  1472. default:
  1473. DRM_ERROR("Unknown table version %d, %d\n", frev, crev);
  1474. return -EINVAL;
  1475. }
  1476. return 0;
  1477. }
  1478. return -EINVAL;
  1479. }
  1480. bool amdgpu_atombios_has_gpu_virtualization_table(struct amdgpu_device *adev)
  1481. {
  1482. int index = GetIndexIntoMasterTable(DATA, GPUVirtualizationInfo);
  1483. u8 frev, crev;
  1484. u16 data_offset, size;
  1485. if (amdgpu_atom_parse_data_header(adev->mode_info.atom_context, index, &size,
  1486. &frev, &crev, &data_offset))
  1487. return true;
  1488. return false;
  1489. }
  1490. void amdgpu_atombios_scratch_regs_lock(struct amdgpu_device *adev, bool lock)
  1491. {
  1492. uint32_t bios_6_scratch;
  1493. bios_6_scratch = RREG32(adev->bios_scratch_reg_offset + 6);
  1494. if (lock) {
  1495. bios_6_scratch |= ATOM_S6_CRITICAL_STATE;
  1496. bios_6_scratch &= ~ATOM_S6_ACC_MODE;
  1497. } else {
  1498. bios_6_scratch &= ~ATOM_S6_CRITICAL_STATE;
  1499. bios_6_scratch |= ATOM_S6_ACC_MODE;
  1500. }
  1501. WREG32(adev->bios_scratch_reg_offset + 6, bios_6_scratch);
  1502. }
  1503. void amdgpu_atombios_scratch_regs_init(struct amdgpu_device *adev)
  1504. {
  1505. uint32_t bios_2_scratch, bios_6_scratch;
  1506. adev->bios_scratch_reg_offset = mmBIOS_SCRATCH_0;
  1507. bios_2_scratch = RREG32(adev->bios_scratch_reg_offset + 2);
  1508. bios_6_scratch = RREG32(adev->bios_scratch_reg_offset + 6);
  1509. /* let the bios control the backlight */
  1510. bios_2_scratch &= ~ATOM_S2_VRI_BRIGHT_ENABLE;
  1511. /* tell the bios not to handle mode switching */
  1512. bios_6_scratch |= ATOM_S6_ACC_BLOCK_DISPLAY_SWITCH;
  1513. /* clear the vbios dpms state */
  1514. bios_2_scratch &= ~ATOM_S2_DEVICE_DPMS_STATE;
  1515. WREG32(adev->bios_scratch_reg_offset + 2, bios_2_scratch);
  1516. WREG32(adev->bios_scratch_reg_offset + 6, bios_6_scratch);
  1517. }
  1518. void amdgpu_atombios_scratch_regs_save(struct amdgpu_device *adev)
  1519. {
  1520. int i;
  1521. for (i = 0; i < AMDGPU_BIOS_NUM_SCRATCH; i++)
  1522. adev->bios_scratch[i] = RREG32(adev->bios_scratch_reg_offset + i);
  1523. }
  1524. void amdgpu_atombios_scratch_regs_restore(struct amdgpu_device *adev)
  1525. {
  1526. int i;
  1527. /*
  1528. * VBIOS will check ASIC_INIT_COMPLETE bit to decide if
  1529. * execute ASIC_Init posting via driver
  1530. */
  1531. adev->bios_scratch[7] &= ~ATOM_S7_ASIC_INIT_COMPLETE_MASK;
  1532. for (i = 0; i < AMDGPU_BIOS_NUM_SCRATCH; i++)
  1533. WREG32(adev->bios_scratch_reg_offset + i, adev->bios_scratch[i]);
  1534. }
  1535. void amdgpu_atombios_scratch_regs_engine_hung(struct amdgpu_device *adev,
  1536. bool hung)
  1537. {
  1538. u32 tmp = RREG32(adev->bios_scratch_reg_offset + 3);
  1539. if (hung)
  1540. tmp |= ATOM_S3_ASIC_GUI_ENGINE_HUNG;
  1541. else
  1542. tmp &= ~ATOM_S3_ASIC_GUI_ENGINE_HUNG;
  1543. WREG32(adev->bios_scratch_reg_offset + 3, tmp);
  1544. }
  1545. bool amdgpu_atombios_scratch_need_asic_init(struct amdgpu_device *adev)
  1546. {
  1547. u32 tmp = RREG32(adev->bios_scratch_reg_offset + 7);
  1548. if (tmp & ATOM_S7_ASIC_INIT_COMPLETE_MASK)
  1549. return false;
  1550. else
  1551. return true;
  1552. }
  1553. /* Atom needs data in little endian format
  1554. * so swap as appropriate when copying data to
  1555. * or from atom. Note that atom operates on
  1556. * dw units.
  1557. */
  1558. void amdgpu_atombios_copy_swap(u8 *dst, u8 *src, u8 num_bytes, bool to_le)
  1559. {
  1560. #ifdef __BIG_ENDIAN
  1561. u8 src_tmp[20], dst_tmp[20]; /* used for byteswapping */
  1562. u32 *dst32, *src32;
  1563. int i;
  1564. memcpy(src_tmp, src, num_bytes);
  1565. src32 = (u32 *)src_tmp;
  1566. dst32 = (u32 *)dst_tmp;
  1567. if (to_le) {
  1568. for (i = 0; i < ((num_bytes + 3) / 4); i++)
  1569. dst32[i] = cpu_to_le32(src32[i]);
  1570. memcpy(dst, dst_tmp, num_bytes);
  1571. } else {
  1572. u8 dws = num_bytes & ~3;
  1573. for (i = 0; i < ((num_bytes + 3) / 4); i++)
  1574. dst32[i] = le32_to_cpu(src32[i]);
  1575. memcpy(dst, dst_tmp, dws);
  1576. if (num_bytes % 4) {
  1577. for (i = 0; i < (num_bytes % 4); i++)
  1578. dst[dws+i] = dst_tmp[dws+i];
  1579. }
  1580. }
  1581. #else
  1582. memcpy(dst, src, num_bytes);
  1583. #endif
  1584. }
  1585. int amdgpu_atombios_allocate_fb_scratch(struct amdgpu_device *adev)
  1586. {
  1587. struct atom_context *ctx = adev->mode_info.atom_context;
  1588. int index = GetIndexIntoMasterTable(DATA, VRAM_UsageByFirmware);
  1589. uint16_t data_offset;
  1590. int usage_bytes = 0;
  1591. struct _ATOM_VRAM_USAGE_BY_FIRMWARE *firmware_usage;
  1592. u64 start_addr;
  1593. u64 size;
  1594. if (amdgpu_atom_parse_data_header(ctx, index, NULL, NULL, NULL, &data_offset)) {
  1595. firmware_usage = (struct _ATOM_VRAM_USAGE_BY_FIRMWARE *)(ctx->bios + data_offset);
  1596. DRM_DEBUG("atom firmware requested %08x %dkb\n",
  1597. le32_to_cpu(firmware_usage->asFirmwareVramReserveInfo[0].ulStartAddrUsedByFirmware),
  1598. le16_to_cpu(firmware_usage->asFirmwareVramReserveInfo[0].usFirmwareUseInKb));
  1599. start_addr = firmware_usage->asFirmwareVramReserveInfo[0].ulStartAddrUsedByFirmware;
  1600. size = firmware_usage->asFirmwareVramReserveInfo[0].usFirmwareUseInKb;
  1601. if ((uint32_t)(start_addr & ATOM_VRAM_OPERATION_FLAGS_MASK) ==
  1602. (uint32_t)(ATOM_VRAM_BLOCK_SRIOV_MSG_SHARE_RESERVATION <<
  1603. ATOM_VRAM_OPERATION_FLAGS_SHIFT)) {
  1604. /* Firmware request VRAM reservation for SR-IOV */
  1605. adev->fw_vram_usage.start_offset = (start_addr &
  1606. (~ATOM_VRAM_OPERATION_FLAGS_MASK)) << 10;
  1607. adev->fw_vram_usage.size = size << 10;
  1608. /* Use the default scratch size */
  1609. usage_bytes = 0;
  1610. } else {
  1611. usage_bytes = le16_to_cpu(firmware_usage->asFirmwareVramReserveInfo[0].usFirmwareUseInKb) * 1024;
  1612. }
  1613. }
  1614. ctx->scratch_size_bytes = 0;
  1615. if (usage_bytes == 0)
  1616. usage_bytes = 20 * 1024;
  1617. /* allocate some scratch memory */
  1618. ctx->scratch = kzalloc(usage_bytes, GFP_KERNEL);
  1619. if (!ctx->scratch)
  1620. return -ENOMEM;
  1621. ctx->scratch_size_bytes = usage_bytes;
  1622. return 0;
  1623. }