blob: e5f79dfe7b55dbd27672ca7e5d90fc4e9e525020 [file] [log] [blame]
Jani Nikula76be7492013-10-08 21:18:13 +03001/*
2 * Copyright © 2013 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * 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 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Jani Nikula <jani.nikula@intel.com>
25 *
26 */
27
28#include <errno.h>
29#include <fcntl.h>
30#include <getopt.h>
31#include <stdint.h>
32#include <stdio.h>
33#include <stdlib.h>
34#include <string.h>
35#include <unistd.h>
36#include <sys/mman.h>
37#include <sys/stat.h>
38#include <sys/types.h>
39
40#include "intel_gpu_tools.h"
41
42#define OPREGION_HEADER_OFFSET 0
43#define OPREGION_ACPI_OFFSET 0x100
44#define OPREGION_SWSCI_OFFSET 0x200
45#define OPREGION_ASLE_OFFSET 0x300
46#define OPREGION_VBT_OFFSET 0x400
47#define OPREGION_ASLE_EXT_OFFSET 0x1C00
48
49#define MBOX_ACPI (1 << 0)
50#define MBOX_SWSCI (1 << 1)
51#define MBOX_ASLE (1 << 2)
52#define MBOX_VBT (1 << 3)
53#define MBOX_ASLE_EXT (1 << 4)
54
55struct opregion_header {
56 char sign[16];
57 uint32_t size;
58 uint32_t over;
59 char sver[32];
60 char vver[16];
61 char gver[16];
62 uint32_t mbox;
63 uint32_t dmod;
64 uint32_t pcon;
65 char dver[32];
66 uint8_t rsv1[124];
67} __attribute__((packed));
68
69/* OpRegion mailbox #1: public ACPI methods */
70struct opregion_acpi {
71 uint32_t drdy; /* driver readiness */
72 uint32_t csts; /* notification status */
73 uint32_t cevt; /* current event */
74 uint8_t rsvd1[20];
75 uint32_t didl[8]; /* supported display devices ID list */
76 uint32_t cpdl[8]; /* currently presented display list */
77 uint32_t cadl[8]; /* currently active display list */
78 uint32_t nadl[8]; /* next active devices list */
79 uint32_t aslp; /* ASL sleep time-out */
80 uint32_t tidx; /* toggle table index */
81 uint32_t chpd; /* current hotplug enable indicator */
82 uint32_t clid; /* current lid state*/
83 uint32_t cdck; /* current docking state */
84 uint32_t sxsw; /* Sx state resume */
85 uint32_t evts; /* ASL supported events */
86 uint32_t cnot; /* current OS notification */
87 uint32_t nrdy; /* driver status */
88 uint32_t did2[7];
89 uint32_t cpd2[7];
90 uint8_t rsvd2[4];
91} __attribute__((packed));
92
93/* OpRegion mailbox #2: SWSCI */
94struct opregion_swsci {
95 uint32_t scic; /* SWSCI command|status|data */
96 uint32_t parm; /* command parameters */
97 uint32_t dslp; /* driver sleep time-out */
98 uint8_t rsvd[244];
99} __attribute__((packed));
100
101/* OpRegion mailbox #3: ASLE */
102struct opregion_asle {
103 uint32_t ardy; /* driver readiness */
104 uint32_t aslc; /* ASLE interrupt command */
105 uint32_t tche; /* technology enabled indicator */
106 uint32_t alsi; /* current ALS illuminance reading */
107 uint32_t bclp; /* backlight brightness to set */
108 uint32_t pfit; /* panel fitting state */
109 uint32_t cblv; /* current brightness level */
110 uint16_t bclm[20]; /* backlight level duty cycle mapping table */
111 uint32_t cpfm; /* current panel fitting mode */
112 uint32_t epfm; /* enabled panel fitting modes */
113 uint8_t plut_header; /* panel LUT and identifier */
114 uint8_t plut_identifier[10]; /* panel LUT and identifier */
115 uint8_t plut[63]; /* panel LUT and identifier */
116 uint32_t pfmb; /* PWM freq and min brightness */
117 uint32_t ccdv;
118 uint32_t pcft;
119 uint32_t srot;
120 uint32_t iuer;
121 uint8_t fdss[8];
122 uint32_t fdsp;
123 uint32_t stat;
124 uint8_t rsvd[86];
125} __attribute__((packed));
126
127/* OpRegion mailbox #4: VBT */
128struct opregion_vbt {
129 char product_string[20];
130 /* rest ignored */
131} __attribute__((packed));
132
133/* OpRegion mailbox #5: ASLE extension */
134struct opregion_asle_ext {
135 uint32_t phed;
136 uint8_t bddc[256];
137} __attribute__((packed));
138
139static uint32_t decode_header(const void *buffer)
140{
141 const struct opregion_header *header = buffer;
142 char *s;
143
144 if (strncmp("IntelGraphicsMem", header->sign, sizeof(header->sign))) {
145 fprintf(stderr, "invalid opregion signature\n");
146 return 0;
147 }
148
149 printf("OpRegion Header:\n");
150
151 s = strndup(header->sign, sizeof(header->sign));
152 printf("\tsign:\t%s\n", s);
153 free(s);
154
155 printf("\tsize:\t0x%08x\n", header->size);
156 printf("\tover:\t0x%08x\n", header->over);
157
158 s = strndup(header->sver, sizeof(header->sver));
159 printf("\tsver:\t%s\n", s);
160 free(s);
161
162 s = strndup(header->vver, sizeof(header->vver));
163 printf("\tvver:\t%s\n", s);
164 free(s);
165
166 s = strndup(header->gver, sizeof(header->gver));
167 printf("\tgver:\t%s\n", s);
168 free(s);
169
170 printf("\tmbox:\t0x%08x\n", header->mbox);
171
172 printf("\tdmod:\t0x%08x\n", header->dmod);
173 printf("\tpcon:\t0x%08x\n", header->pcon);
174
175 s = strndup(header->dver, sizeof(header->dver));
176 printf("\tdver:\t%s\n", s);
177 free(s);
178
179 printf("\n");
180
181 return header->mbox;
182}
183
184static void decode_acpi(const void *buffer)
185{
186 const struct opregion_acpi *acpi = buffer;
187 int i;
188
189 printf("OpRegion Mailbox 1: Public ACPI Methods:\n");
190
191 printf("\tdrdy:\t0x%08x\n", acpi->drdy);
192 printf("\tcsts:\t0x%08x\n", acpi->csts);
193 printf("\tcevt:\t0x%08x\n", acpi->cevt);
194
195 printf("\tdidl:\n");
196 for (i = 0; i < ARRAY_SIZE(acpi->didl); i++)
197 printf("\t\tdidl[%d]:\t0x%08x\n", i, acpi->didl[i]);
198
199 printf("\tcpdl:\n");
200 for (i = 0; i < ARRAY_SIZE(acpi->cpdl); i++)
201 printf("\t\tcpdl[%d]:\t0x%08x\n", i, acpi->cpdl[i]);
202
203 printf("\tcadl:\n");
204 for (i = 0; i < ARRAY_SIZE(acpi->cadl); i++)
205 printf("\t\tcadl[%d]:\t0x%08x\n", i, acpi->cadl[i]);
206
207 printf("\tnadl:\n");
208 for (i = 0; i < ARRAY_SIZE(acpi->nadl); i++)
209 printf("\t\tnadl[%d]:\t0x%08x\n", i, acpi->nadl[i]);
210
211 printf("\taslp:\t0x%08x\n", acpi->aslp);
212 printf("\ttidx:\t0x%08x\n", acpi->tidx);
213 printf("\tchpd:\t0x%08x\n", acpi->chpd);
214 printf("\tclid:\t0x%08x\n", acpi->clid);
215 printf("\tcdck:\t0x%08x\n", acpi->cdck);
216 printf("\tsxsw:\t0x%08x\n", acpi->sxsw);
217 printf("\tevts:\t0x%08x\n", acpi->evts);
218 printf("\tcnot:\t0x%08x\n", acpi->cnot);
219 printf("\tnrdy:\t0x%08x\n", acpi->nrdy);
220
221 printf("\tdid2:\n");
222 for (i = 0; i < ARRAY_SIZE(acpi->did2); i++)
223 printf("\t\tdid2[%d]:\t0x%08x\n", i, acpi->did2[i]);
224
225 printf("\tcpd2:\n");
226 for (i = 0; i < ARRAY_SIZE(acpi->cpd2); i++)
227 printf("\t\tcpd2[%d]:\t0x%08x\n", i, acpi->cpd2[i]);
228
229 printf("\n");
230}
231
232static void decode_swsci(const void *buffer)
233{
234 const struct opregion_swsci *swsci = buffer;
235
236 printf("OpRegion Mailbox 2: Software SCI Interface (SWSCI):\n");
237
238 printf("\tscic:\t0x%08x\n", swsci->scic);
239 printf("\tparm:\t0x%08x\n", swsci->parm);
240 printf("\tdslp:\t0x%08x\n", swsci->dslp);
241
242 printf("\n");
243}
244
245static void decode_asle(const void *buffer)
246{
247 const struct opregion_asle *asle = buffer;
248 int i;
249
250 printf("OpRegion Mailbox 3: BIOS to Driver Notification (ASLE):\n");
251
252 printf("\tardy:\t0x%08x\n", asle->ardy);
253 printf("\taslc:\t0x%08x\n", asle->aslc);
254 printf("\ttche:\t0x%08x\n", asle->tche);
255 printf("\talsi:\t0x%08x\n", asle->alsi);
256 printf("\tbclp:\t0x%08x\n", asle->bclp);
257 printf("\tpfit:\t0x%08x\n", asle->pfit);
258 printf("\tcblv:\t0x%08x\n", asle->cblv);
259
260 printf("\tbclm:\n");
261 for (i = 0; i < ARRAY_SIZE(asle->bclm); i++)
262 printf("\t\tbclm[%d]:\t0x%04x\n", i, asle->bclm[i]);
263
264 printf("\tcpfm:\t0x%08x\n", asle->cpfm);
265 printf("\tepfm:\t0x%08x\n", asle->epfm);
266
267 printf("\tplut header:\t0x%02x\n", asle->plut_header);
268
269 printf("\tplut identifier:");
270 for (i = 0; i < ARRAY_SIZE(asle->plut_identifier); i++)
271 printf(" %02x", asle->plut_identifier[i]);
272 printf("\n");
273
274 printf("\tplut:\n");
275 for (i = 0; i < ARRAY_SIZE(asle->plut); i++) {
276 const int COLUMNS = 7;
277
278 if (i % COLUMNS == 0)
279 printf("\t\tplut[%d]:\t", i / COLUMNS);
280
281 printf("%02x ", asle->plut[i]);
282
283 if (i % COLUMNS == COLUMNS - 1)
284 printf("\n");
285 }
286
287 printf("\tpfmb:\t0x%08x\n", asle->pfmb);
288 printf("\tccdv:\t0x%08x\n", asle->ccdv);
289 printf("\tpcft:\t0x%08x\n", asle->pcft);
290 printf("\tsrot:\t0x%08x\n", asle->srot);
291 printf("\tiuer:\t0x%08x\n", asle->iuer);
292
293 printf("\tfdss:\t");
294 for (i = 0; i < ARRAY_SIZE(asle->fdss); i++)
295 printf("%02x ", asle->fdss[i]);
296 printf("\n");
297
298 printf("\tfdsp:\t0x%08x\n", asle->fdsp);
299 printf("\tstat:\t0x%08x\n", asle->stat);
300
301 printf("\n");
302}
303
304static void decode_vbt(const void *buffer)
305{
306 const struct opregion_vbt *vbt = buffer;
307 char *s;
308
309 printf("OpRegion Mailbox 4: Video BIOS Table (VBT):\n");
310
311 s = strndup(vbt->product_string, sizeof(vbt->product_string));
312 printf("\tproduct string:\t%s\n", s);
313 free(s);
314
315 printf("\t(use intel_bios_reader to decode the VBT)\n");
316
317 printf("\n");
318}
319
320static void decode_asle_ext(const void *buffer)
321{
322 const struct opregion_asle_ext *asle_ext = buffer;
323 int i;
324
325 printf("OpRegion Mailbox 5: BIOS to Driver Notification Extension:\n");
326
327 printf("\tphed:\t0x%08x\n", asle_ext->phed);
328
329 printf("\tbddc:\n");
330 for (i = 0; i < ARRAY_SIZE(asle_ext->bddc); i++) {
331 const int COLUMNS = 16;
332
333 if (i % COLUMNS == 0)
334 printf("\t\tbddc[0x%02x]:\t", i);
335
336 printf("%02x ", asle_ext->bddc[i]);
337
338 if (i % COLUMNS == COLUMNS - 1)
339 printf("\n");
340 }
341
342 printf("\n");
343}
344
345static void decode_opregion(const uint8_t *opregion, int size)
346{
347 uint32_t mbox;
348
349 /* XXX: allow decoding up to size */
350 if (OPREGION_ASLE_EXT_OFFSET + sizeof(struct opregion_asle_ext) > size) {
351 fprintf(stderr, "buffer too small\n");
352 return;
353 }
354
355 mbox = decode_header(opregion + OPREGION_HEADER_OFFSET);
356 if (mbox & MBOX_ACPI)
357 decode_acpi(opregion + OPREGION_ACPI_OFFSET);
358 if (mbox & MBOX_SWSCI)
359 decode_swsci(opregion + OPREGION_SWSCI_OFFSET);
360 if (mbox & MBOX_ASLE)
361 decode_asle(opregion + OPREGION_ASLE_OFFSET);
362 if (mbox & MBOX_VBT)
363 decode_vbt(opregion + OPREGION_VBT_OFFSET);
364 if (mbox & MBOX_ASLE_EXT)
365 decode_asle_ext(opregion + OPREGION_ASLE_EXT_OFFSET);
366}
367
368int main(int argc, char *argv[])
369{
370 const char *filename = "/sys/kernel/debug/dri/0/i915_opregion";
371 int fd;
372 struct stat finfo;
373 uint8_t *opregion;
374 int c, option_index = 0;
375
376 static struct option long_options[] = {
377 { "file", required_argument, 0, 'f' },
378 { "help", no_argument, 0, 'h' },
379 { 0 },
380 };
381
382 while ((c = getopt_long(argc, argv, "hf:",
383 long_options, &option_index)) != -1) {
384 switch (c) {
385 case 'h':
386 printf("usage: intel_opregion_decode [-f|--file=<input>]\n");
387 return 0;
388 case 'f':
389 filename = optarg;
390 break;
391 default:
392 fprintf(stderr, "unkown command options\n");
393 return 1;
394 }
395 }
396
397 fd = open(filename, O_RDONLY);
398 if (fd == -1) {
399 printf("Couldn't open \"%s\": %s\n", filename, strerror(errno));
400 return 1;
401 }
402
403 if (stat(filename, &finfo)) {
404 printf("failed to stat \"%s\": %s\n", filename, strerror(errno));
405 return 1;
406 }
407
408 if (finfo.st_size == 0) {
409 int len = 0, ret;
410 finfo.st_size = 8192;
411 opregion = malloc(finfo.st_size);
412 while ((ret = read(fd, opregion + len, finfo.st_size - len))) {
413 if (ret < 0) {
414 printf("failed to read \"%s\": %s\n", filename,
415 strerror(errno));
416 return 1;
417 }
418
419 len += ret;
420 if (len == finfo.st_size) {
421 finfo.st_size *= 2;
422 opregion = realloc(opregion, finfo.st_size);
423 }
424 }
425 } else {
426 opregion = mmap(NULL, finfo.st_size, PROT_READ, MAP_SHARED,
427 fd, 0);
428 if (opregion == MAP_FAILED) {
429 printf("failed to map \"%s\": %s\n", filename,
430 strerror(errno));
431 return 1;
432 }
433 }
434
435 decode_opregion(opregion, finfo.st_size);
436
437 return 0;
438}