xref: /haiku/src/add-ons/accelerants/intel_extreme/accelerant.cpp (revision 9e25244c5e9051f6cd333820d6332397361abd6c)
1 /*
2  * Copyright 2006-2016, Haiku, Inc. All Rights Reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Axel Dörfler, axeld@pinc-software.de
7  */
8 
9 
10 #include "accelerant_protos.h"
11 #include "accelerant.h"
12 
13 #include "utility.h"
14 
15 #include <Debug.h>
16 #include <errno.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <unistd.h>
20 #include <syslog.h>
21 
22 #include <new>
23 
24 #include <AGP.h>
25 #include <AutoDeleterOS.h>
26 
27 
28 #undef TRACE
29 #define TRACE_ACCELERANT
30 #ifdef TRACE_ACCELERANT
31 #	define TRACE(x...) _sPrintf("intel_extreme: " x)
32 #else
33 #	define TRACE(x...)
34 #endif
35 
36 #define ERROR(x...) _sPrintf("intel_extreme: " x)
37 #define CALLED(x...) TRACE("CALLED %s\n", __PRETTY_FUNCTION__)
38 
39 
40 struct accelerant_info* gInfo;
41 uint32 gDumpCount;
42 
43 
44 //	#pragma mark -
45 
46 
47 // intel_reg --mmio=ie-0001.bin --devid=27a2 dump
48 void
49 dump_registers()
50 {
51 	char filename[255];
52 
53 	sprintf(filename, "/boot/system/cache/tmp/ie-%04" B_PRId32 ".bin",
54 		gDumpCount);
55 
56 	ERROR("%s: Taking register dump #%" B_PRId32 "\n", __func__, gDumpCount);
57 
58 	int fd = open(filename, O_CREAT | O_WRONLY, 0644);
59 	uint32 data = 0;
60 	if (fd >= 0) {
61 		for (int32 i = 0; i < 0x80000; i += sizeof(data)) {
62 			//char line[512];
63 			//int length = sprintf(line, "%05" B_PRIx32 ": "
64 			//	"%08" B_PRIx32 " %08" B_PRIx32 " %08" B_PRIx32 " %08" B_PRIx32 "\n",
65 			//	i, read32(i), read32(i + 4), read32(i + 8), read32(i + 12));
66 			data = read32(i);
67 			write(fd, &data, sizeof(data));
68 		}
69 		close(fd);
70 		sync();
71 	}
72 
73 	gDumpCount++;
74 }
75 
76 
77 /*! This is the common accelerant_info initializer. It is called by
78 	both, the first accelerant and all clones.
79 */
80 static status_t
81 init_common(int device, bool isClone)
82 {
83 	// initialize global accelerant info structure
84 
85 	// Number of register dumps we have... taken.
86 	gDumpCount = 0;
87 
88 	gInfo = (accelerant_info*)malloc(sizeof(accelerant_info));
89 	if (gInfo == NULL)
90 		return B_NO_MEMORY;
91 	MemoryDeleter infoDeleter(gInfo);
92 
93 	memset(gInfo, 0, sizeof(accelerant_info));
94 
95 	gInfo->is_clone = isClone;
96 	gInfo->device = device;
97 
98 	// get basic info from driver
99 
100 	intel_get_private_data data;
101 	data.magic = INTEL_PRIVATE_DATA_MAGIC;
102 
103 	if (ioctl(device, INTEL_GET_PRIVATE_DATA, &data,
104 			sizeof(intel_get_private_data)) != 0)
105 		return B_ERROR;
106 
107 	AreaDeleter sharedDeleter(clone_area("intel extreme shared info",
108 		(void**)&gInfo->shared_info, B_ANY_ADDRESS, B_READ_AREA | B_WRITE_AREA,
109 		data.shared_info_area));
110 	status_t status = gInfo->shared_info_area = sharedDeleter.Get();
111 	if (status < B_OK)
112 		return status;
113 
114 	AreaDeleter regsDeleter(clone_area("intel extreme regs",
115 		(void**)&gInfo->registers, B_ANY_ADDRESS, B_READ_AREA | B_WRITE_AREA,
116 		gInfo->shared_info->registers_area));
117 	status = gInfo->regs_area = regsDeleter.Get();
118 	if (status < B_OK)
119 		return status;
120 
121 	infoDeleter.Detach();
122 	sharedDeleter.Detach();
123 	regsDeleter.Detach();
124 
125 	// The overlay registers, hardware status, and cursor memory share
126 	// a single area with the shared_info
127 
128 	if (gInfo->shared_info->overlay_offset != 0) {
129 		gInfo->overlay_registers = (struct overlay_registers*)
130 			(gInfo->shared_info->graphics_memory
131 			+ gInfo->shared_info->overlay_offset);
132 	}
133 
134 	if (gInfo->shared_info->device_type.InGroup(INTEL_GROUP_96x)) {
135 		// allocate some extra memory for the 3D context
136 		if (intel_allocate_memory(INTEL_i965_3D_CONTEXT_SIZE,
137 				B_APERTURE_NON_RESERVED, gInfo->context_base) == B_OK) {
138 			gInfo->context_offset = gInfo->context_base
139 				- (addr_t)gInfo->shared_info->graphics_memory;
140 		}
141 	}
142 
143 	gInfo->pipe_count = 0;
144 
145 	// Allocate all of our pipes
146 	for (int i = 0; i < MAX_PIPES; i++) {
147 		switch (i) {
148 			case 0:
149 				gInfo->pipes[i] = new(std::nothrow) Pipe(INTEL_PIPE_A);
150 				break;
151 			case 1:
152 				gInfo->pipes[i] = new(std::nothrow) Pipe(INTEL_PIPE_B);
153 				break;
154 			default:
155 				ERROR("%s: Unknown pipe %d\n", __func__, i);
156 		}
157 		if (gInfo->pipes[i] == NULL)
158 			ERROR("%s: Error allocating pipe %d\n", __func__, i);
159 		else
160 			gInfo->pipe_count++;
161 	}
162 
163 	return B_OK;
164 }
165 
166 
167 /*! Clean up data common to both primary and cloned accelerant */
168 static void
169 uninit_common(void)
170 {
171 	intel_free_memory(gInfo->context_base);
172 
173 	delete_area(gInfo->regs_area);
174 	delete_area(gInfo->shared_info_area);
175 
176 	gInfo->regs_area = gInfo->shared_info_area = -1;
177 
178 	// close the file handle ONLY if we're the clone
179 	if (gInfo->is_clone)
180 		close(gInfo->device);
181 
182 	free(gInfo);
183 }
184 
185 
186 static void
187 dump_ports()
188 {
189 	if (gInfo->port_count == 0) {
190 		TRACE("%s: No ports connected\n", __func__);
191 		return;
192 	}
193 
194 	TRACE("%s: Connected ports: (port_count: %" B_PRIu32 ")\n", __func__,
195 		gInfo->port_count);
196 
197 	for (uint32 i = 0; i < gInfo->port_count; i++) {
198 		Port* port = gInfo->ports[i];
199 		if (!port) {
200 			TRACE("port %" B_PRIu32 ":: INVALID ALLOC!\n", i);
201 			continue;
202 		}
203 		TRACE("port %" B_PRIu32 ": %s %s\n", i, port->PortName(),
204 			port->IsConnected() ? "connected" : "disconnected");
205 	}
206 }
207 
208 
209 static bool
210 has_connected_port(port_index portIndex, uint32 type)
211 {
212 	for (uint32 i = 0; i < gInfo->port_count; i++) {
213 		Port* port = gInfo->ports[i];
214 		if (type != INTEL_PORT_TYPE_ANY && port->Type() != type)
215 			continue;
216 		if (portIndex != INTEL_PORT_ANY && port->PortIndex() != portIndex)
217 			continue;
218 
219 		return true;
220 	}
221 
222 	return false;
223 }
224 
225 
226 static status_t
227 probe_ports()
228 {
229 	// Try to determine what ports to use. We use the following heuristic:
230 	// * Check for DisplayPort, these can be more or less detected reliably.
231 	// * Check for HDMI, it'll fail on devices not having HDMI for us to fall
232 	//   back to DVI.
233 	// * Assume DVI B if no HDMI and no DisplayPort is present, confirmed by
234 	//   reading EDID in the IsConnected() call.
235 	// * Check for analog if possible (there's a detection bit on PCH),
236 	//   otherwise the assumed presence is confirmed by reading EDID in
237 	//   IsConnected().
238 
239 	TRACE("adpa: %08" B_PRIx32 "\n", read32(INTEL_ANALOG_PORT));
240 	TRACE("dova: %08" B_PRIx32 ", dovb: %08" B_PRIx32
241 		", dovc: %08" B_PRIx32 "\n", read32(INTEL_DIGITAL_PORT_A),
242 		read32(INTEL_DIGITAL_PORT_B), read32(INTEL_DIGITAL_PORT_C));
243 	TRACE("lvds: %08" B_PRIx32 "\n", read32(INTEL_DIGITAL_LVDS_PORT));
244 
245 	TRACE("dp_a: %08" B_PRIx32 "\n", read32(INTEL_DISPLAY_PORT_A));
246 	TRACE("dp_b: %08" B_PRIx32 "\n", read32(INTEL_DISPLAY_PORT_B));
247 	TRACE("dp_c: %08" B_PRIx32 "\n", read32(INTEL_DISPLAY_PORT_C));
248 	TRACE("dp_d: %08" B_PRIx32 "\n", read32(INTEL_DISPLAY_PORT_D));
249 	TRACE("tra_dp: %08" B_PRIx32 "\n", read32(INTEL_TRANSCODER_A_DP_CTL));
250 	TRACE("trb_dp: %08" B_PRIx32 "\n", read32(INTEL_TRANSCODER_B_DP_CTL));
251 	TRACE("trc_dp: %08" B_PRIx32 "\n", read32(INTEL_TRANSCODER_C_DP_CTL));
252 
253 	bool foundLVDS = false;
254 	bool foundDP = false;
255 	bool foundDDI = false;
256 
257 	gInfo->port_count = 0;
258 #if 0
259 	// make sure I2C hardware controller is off (we use bit-banging)
260 	if (gInfo->shared_info->device_type.Generation() >= 5) {
261 		write32(INTEL_DSPCLK_GATE_D,
262 			read32(INTEL_DSPCLK_GATE_D) | PCH_GMBUSUNIT_CLK_GATE_DIS);
263 		read32(INTEL_DSPCLK_GATE_D);
264 
265 		write32(INTEL_GEN9_CLKGATE_DIS_4,
266 			read32(INTEL_GEN9_CLKGATE_DIS_4) | BXT_GMBUSUNIT_CLK_GATE_DIS);
267 		read32(INTEL_GEN9_CLKGATE_DIS_4);
268 
269 		write32(INTEL_GMBUS0, 0); //reset, idle
270 		write32(INTEL_GMBUS4, 0); //block interrupts
271 	}
272 #endif
273 
274 	// Display Port
275 	if (!gInfo->shared_info->device_type.HasDDI()) {
276 		for (int i = INTEL_PORT_A; i <= INTEL_PORT_D; i++) {
277 			TRACE("Probing DisplayPort %d\n", i);
278 			Port* displayPort = new(std::nothrow) DisplayPort((port_index)i);
279 			if (displayPort == NULL)
280 				return B_NO_MEMORY;
281 
282 			if (displayPort->IsConnected()) {
283 				foundDP = true;
284 				gInfo->ports[gInfo->port_count++] = displayPort;
285 			} else
286 				delete displayPort;
287 		}
288 	}
289 
290 	// Digital Display Interface (for DP, HDMI, DVI and eDP)
291 	if (gInfo->shared_info->device_type.HasDDI()) {
292 		for (int i = INTEL_PORT_B; i <= INTEL_PORT_F; i++) {
293 			TRACE("Probing DDI %d\n", i);
294 
295 			Port* ddiPort
296 				= new(std::nothrow) DigitalDisplayInterface((port_index)i);
297 
298 			if (ddiPort == NULL)
299 				return B_NO_MEMORY;
300 
301 			if (ddiPort->IsConnected()) {
302 				foundDDI = true;
303 				gInfo->ports[gInfo->port_count++] = ddiPort;
304 			} else
305 				delete ddiPort;
306 		}
307 	}
308 
309 #if 0
310 	// never execute this as the 'standard' DisplayPort class called above already handles it.
311 	if (!gInfo->shared_info->device_type.HasDDI()) {
312 		// Ensure DP_A isn't already taken
313 		TRACE("Probing eDP\n");
314 		if (!has_connected_port((port_index)INTEL_PORT_A, INTEL_PORT_TYPE_ANY)) {
315 			// also always try eDP, it'll also just fail if not applicable
316 			Port* eDPPort = new(std::nothrow) EmbeddedDisplayPort();
317 			if (eDPPort == NULL)
318 				return B_NO_MEMORY;
319 			if (eDPPort->IsConnected())
320 				gInfo->ports[gInfo->port_count++] = eDPPort;
321 			else
322 				delete eDPPort;
323 		}
324 	}
325 #endif
326 
327 	if (!gInfo->shared_info->device_type.HasDDI()) {
328 		for (int i = INTEL_PORT_B; i <= INTEL_PORT_D; i++) {
329 			TRACE("Probing HDMI %d\n", i);
330 			if (has_connected_port((port_index)i, INTEL_PORT_TYPE_ANY)) {
331 				// Ensure port not already claimed by something like DDI
332 				TRACE("Port already claimed\n");
333 				continue;
334 			}
335 
336 			Port* hdmiPort = new(std::nothrow) HDMIPort((port_index)i);
337 			if (hdmiPort == NULL)
338 				return B_NO_MEMORY;
339 
340 			if (hdmiPort->IsConnected())
341 				gInfo->ports[gInfo->port_count++] = hdmiPort;
342 			else
343 				delete hdmiPort;
344 		}
345 	}
346 
347 	// always try the LVDS port when chipset supports it, it'll simply fail if not applicable
348 	if (!gInfo->shared_info->device_type.HasDDI()) {
349 		TRACE("Probing LVDS\n");
350 		Port* lvdsPort = new(std::nothrow) LVDSPort();
351 		if (lvdsPort == NULL)
352 			return B_NO_MEMORY;
353 		if (lvdsPort->IsConnected()) {
354 			foundLVDS = true;
355 			gInfo->ports[gInfo->port_count++] = lvdsPort;
356 			gInfo->head_mode |= HEAD_MODE_LVDS_PANEL;
357 			gInfo->head_mode |= HEAD_MODE_B_DIGITAL;
358 		} else
359 			delete lvdsPort;
360 	}
361 
362 	if (!gInfo->shared_info->device_type.HasDDI()) {
363 		if (!has_connected_port(INTEL_PORT_ANY, INTEL_PORT_TYPE_ANY)) {
364 			TRACE("Probing DVI\n");
365 			// there's neither DisplayPort nor HDMI so far, assume DVI B
366 			for (port_index index = INTEL_PORT_B; index <= INTEL_PORT_C;
367 					index = (port_index)(index + 1)) {
368 				Port* dviPort = new(std::nothrow) DigitalPort(index, "DVI");
369 				if (dviPort == NULL)
370 					return B_NO_MEMORY;
371 
372 				if (dviPort->IsConnected()) {
373 					gInfo->ports[gInfo->port_count++] = dviPort;
374 					gInfo->head_mode |= HEAD_MODE_B_DIGITAL;
375 				} else
376 					delete dviPort;
377 			}
378 		}
379 	}
380 
381 	// then finally always try the analog port when chipsets supports it
382 	if (gInfo->shared_info->device_type.Generation() <= 8) {
383 		TRACE("Probing Analog\n");
384 		Port* analogPort = new(std::nothrow) AnalogPort();
385 		if (analogPort == NULL)
386 			return B_NO_MEMORY;
387 		if (analogPort->IsConnected()) {
388 			gInfo->ports[gInfo->port_count++] = analogPort;
389 			gInfo->head_mode |= HEAD_MODE_A_ANALOG;
390 		} else
391 			delete analogPort;
392 	}
393 
394 	if (gInfo->port_count == 0)
395 		return B_ERROR;
396 
397 	// Activate reference clocks if needed
398 	if (gInfo->shared_info->pch_info == INTEL_PCH_IBX
399 		|| gInfo->shared_info->pch_info == INTEL_PCH_CPT) {
400 		TRACE("Activating clocks\n");
401 		refclk_activate_ilk(foundLVDS || foundDP || foundDDI);
402 	}
403 	/*
404 	} else if (gInfo->shared_info->pch_info == INTEL_PCH_LPT) {
405 		// TODO: Some kind of stepped bend thing?
406 		// only needed for vga
407 		refclk_activate_lpt(foundLVDS);
408 	}
409 	*/
410 
411 	TRACE("Probing complete.\n");
412 	return B_OK;
413 }
414 
415 
416 static status_t
417 assign_pipes()
418 {
419 	// TODO: At some point we should "group" ports to pipes with the same mode.
420 	// You can drive multiple ports from a single pipe as long as the mode is
421 	// the same. For the moment we could get displays with the wrong pipes
422 	// assigned when the count is > 1;
423 
424 	uint32 current = 0;
425 
426 	bool assigned[gInfo->pipe_count];
427 	memset(assigned, 0, gInfo->pipe_count);
428 
429 	// Some ports need to be assigned to a fixed pipe on old hardware (or due
430 	// to limitations in the current driver on current hardware). Assign those
431 	// first
432 	for (uint32 i = 0; i < gInfo->port_count; i++) {
433 		if (!gInfo->ports[i]->IsConnected())
434 			continue;
435 
436 		pipe_index preference = gInfo->ports[i]->PipePreference();
437 		if (preference != INTEL_PIPE_ANY) {
438 			int index = (int)preference - 1;
439 			if (assigned[index]) {
440 				TRACE("Pipe %d is already assigned, it will drive multiple "
441 					"displays\n", index);
442 			}
443 			gInfo->ports[i]->SetPipe(gInfo->pipes[index]);
444 			assigned[index] = true;
445 			continue;
446 		}
447 	}
448 
449 	// In a second pass, assign the remaining ports to the remaining pipes
450 	for (uint32 i = 0; i < gInfo->port_count; i++) {
451 		if (gInfo->ports[i]->IsConnected() && gInfo->ports[i]->GetPipe() == NULL) {
452 			while (current < gInfo->pipe_count && assigned[current])
453 				current++;
454 
455 			if (current >= gInfo->pipe_count) {
456 				ERROR("%s: No pipes left to assign to port %s!\n", __func__,
457 					gInfo->ports[i]->PortName());
458 				continue;
459 			}
460 
461 			gInfo->ports[i]->SetPipe(gInfo->pipes[current]);
462 			assigned[current] = true;
463 		}
464 	}
465 
466 	return B_OK;
467 }
468 
469 
470 //	#pragma mark - public accelerant functions
471 
472 
473 /*! Init primary accelerant */
474 status_t
475 intel_init_accelerant(int device)
476 {
477 	CALLED();
478 
479 	status_t status = init_common(device, false);
480 	if (status != B_OK)
481 		return status;
482 
483 	intel_shared_info &info = *gInfo->shared_info;
484 
485 	init_lock(&info.accelerant_lock, "intel extreme accelerant");
486 	init_lock(&info.engine_lock, "intel extreme engine");
487 
488 	setup_ring_buffer(info.primary_ring_buffer, "intel primary ring buffer");
489 
490 	// Probe all ports
491 	status = probe_ports();
492 
493 	// On TRACE, dump ports and states
494 	dump_ports();
495 
496 	if (status != B_OK)
497 		ERROR("Warning: zero active displays were found!\n");
498 
499 	status = assign_pipes();
500 
501 	if (status != B_OK)
502 		ERROR("Warning: error while assigning pipes!\n");
503 
504 	status = create_mode_list();
505 	if (status != B_OK) {
506 		uninit_common();
507 		return status;
508 	}
509 
510 	return B_OK;
511 }
512 
513 
514 ssize_t
515 intel_accelerant_clone_info_size(void)
516 {
517 	CALLED();
518 	// clone info is device name, so return its maximum size
519 	return B_PATH_NAME_LENGTH;
520 }
521 
522 
523 void
524 intel_get_accelerant_clone_info(void* info)
525 {
526 	CALLED();
527 	ioctl(gInfo->device, INTEL_GET_DEVICE_NAME, info, B_PATH_NAME_LENGTH);
528 }
529 
530 
531 status_t
532 intel_clone_accelerant(void* info)
533 {
534 	CALLED();
535 
536 	// create full device name
537 	char path[B_PATH_NAME_LENGTH];
538 	strcpy(path, "/dev/");
539 #ifdef __HAIKU__
540 	strlcat(path, (const char*)info, sizeof(path));
541 #else
542 	strcat(path, (const char*)info);
543 #endif
544 
545 	int fd = open(path, B_READ_WRITE);
546 	if (fd < 0)
547 		return errno;
548 
549 	status_t status = init_common(fd, true);
550 	if (status != B_OK)
551 		goto err1;
552 
553 	// get read-only clone of supported display modes
554 	status = gInfo->mode_list_area = clone_area(
555 		"intel extreme cloned modes", (void**)&gInfo->mode_list,
556 		B_ANY_ADDRESS, B_READ_AREA, gInfo->shared_info->mode_list_area);
557 	if (status < B_OK)
558 		goto err2;
559 
560 	return B_OK;
561 
562 err2:
563 	uninit_common();
564 err1:
565 	close(fd);
566 	return status;
567 }
568 
569 
570 /*! This function is called for both, the primary accelerant and all of
571 	its clones.
572 */
573 void
574 intel_uninit_accelerant(void)
575 {
576 	CALLED();
577 
578 	// delete accelerant instance data
579 	delete_area(gInfo->mode_list_area);
580 	gInfo->mode_list = NULL;
581 
582 	if (!gInfo->is_clone) {
583 		intel_shared_info &info = *gInfo->shared_info;
584 		uninit_lock(&info.accelerant_lock);
585 		uninit_lock(&info.engine_lock);
586 		uninit_ring_buffer(info.primary_ring_buffer);
587 	}
588 	uninit_common();
589 }
590 
591 
592 status_t
593 intel_get_accelerant_device_info(accelerant_device_info* info)
594 {
595 	CALLED();
596 
597 	info->version = B_ACCELERANT_VERSION;
598 
599 	DeviceType* type = &gInfo->shared_info->device_type;
600 
601 	if (type->InFamily(INTEL_FAMILY_8xx))
602 		strcpy(info->name, "Intel Extreme");
603 	else if (type->InFamily(INTEL_FAMILY_9xx))
604 		strcpy(info->name, "Intel GMA");
605 	else if (type->InFamily(INTEL_FAMILY_SOC0))
606 		strcpy(info->name, "Intel Atom");
607 	else if (type->InFamily(INTEL_FAMILY_SER5))
608 		strcpy(info->name, "Intel HD/Iris");
609 	else
610 		strcpy(info->name, "Intel");
611 
612 	strcpy(info->chipset, gInfo->shared_info->device_identifier);
613 	strcpy(info->serial_no, "None");
614 
615 	info->memory = gInfo->shared_info->graphics_memory_size;
616 	info->dac_speed = gInfo->shared_info->pll_info.max_frequency;
617 
618 	return B_OK;
619 }
620 
621 
622 sem_id
623 intel_accelerant_retrace_semaphore()
624 {
625 	CALLED();
626 	return gInfo->shared_info->vblank_sem;
627 }
628