xref: /haiku/src/add-ons/accelerants/intel_extreme/accelerant.cpp (revision 02354704729d38c3b078c696adc1bbbd33cbcf72)
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 
259 	// Display Port
260 	if (!gInfo->shared_info->device_type.HasDDI()) {
261 		for (int i = INTEL_PORT_A; i <= INTEL_PORT_D; i++) {
262 			TRACE("Probing DisplayPort %d\n", i);
263 			Port* displayPort = new(std::nothrow) DisplayPort((port_index)i);
264 			if (displayPort == NULL)
265 				return B_NO_MEMORY;
266 
267 			if (displayPort->IsConnected()) {
268 				foundDP = true;
269 				gInfo->ports[gInfo->port_count++] = displayPort;
270 			} else
271 				delete displayPort;
272 		}
273 	}
274 
275 	// Digital Display Interface
276 	if (gInfo->shared_info->device_type.HasDDI()) {
277 		for (int i = INTEL_PORT_A; i <= INTEL_PORT_E; i++) {
278 			TRACE("Probing DDI %d\n", i);
279 
280 			Port* ddiPort
281 				= new(std::nothrow) DigitalDisplayInterface((port_index)i);
282 
283 			if (ddiPort == NULL)
284 				return B_NO_MEMORY;
285 
286 			if (ddiPort->IsConnected()) {
287 				foundDDI = true;
288 				gInfo->ports[gInfo->port_count++] = ddiPort;
289 			} else
290 				delete ddiPort;
291 		}
292 	}
293 
294 	// Ensure DP_A isn't already taken (or DDI)
295 	TRACE("Probing eDP\n");
296 	if (!has_connected_port((port_index)INTEL_PORT_A, INTEL_PORT_TYPE_ANY)) {
297 		// also always try eDP, it'll also just fail if not applicable
298 		Port* eDPPort = new(std::nothrow) EmbeddedDisplayPort();
299 		if (eDPPort == NULL)
300 			return B_NO_MEMORY;
301 		if (eDPPort->IsConnected())
302 			gInfo->ports[gInfo->port_count++] = eDPPort;
303 		else
304 			delete eDPPort;
305 	}
306 
307 	for (int i = INTEL_PORT_B; i <= INTEL_PORT_D; i++) {
308 		TRACE("Probing HDMI %d\n", i);
309 		if (has_connected_port((port_index)i, INTEL_PORT_TYPE_ANY)) {
310 			// Ensure port not already claimed by something like DDI
311 			TRACE("Port already claimed\n");
312 			continue;
313 		}
314 
315 		Port* hdmiPort = new(std::nothrow) HDMIPort((port_index)i);
316 		if (hdmiPort == NULL)
317 			return B_NO_MEMORY;
318 
319 		if (hdmiPort->IsConnected())
320 			gInfo->ports[gInfo->port_count++] = hdmiPort;
321 		else
322 			delete hdmiPort;
323 	}
324 
325 	// always try the LVDS port, it'll simply fail if not applicable
326 	TRACE("Probing LVDS\n");
327 	Port* lvdsPort = new(std::nothrow) LVDSPort();
328 	if (lvdsPort == NULL)
329 		return B_NO_MEMORY;
330 	if (lvdsPort->IsConnected()) {
331 		foundLVDS = true;
332 		gInfo->ports[gInfo->port_count++] = lvdsPort;
333 		gInfo->head_mode |= HEAD_MODE_LVDS_PANEL;
334 		gInfo->head_mode |= HEAD_MODE_B_DIGITAL;
335 	} else
336 		delete lvdsPort;
337 
338 	if (!has_connected_port(INTEL_PORT_ANY, INTEL_PORT_TYPE_ANY)) {
339 		TRACE("Probing DVI\n");
340 		// there's neither DisplayPort nor HDMI so far, assume DVI B
341 		for (port_index index = INTEL_PORT_B; index <= INTEL_PORT_C;
342 				index = (port_index)(index + 1)) {
343 			Port* dviPort = new(std::nothrow) DigitalPort(index, "DVI");
344 			if (dviPort == NULL)
345 				return B_NO_MEMORY;
346 
347 			if (dviPort->IsConnected()) {
348 				gInfo->ports[gInfo->port_count++] = dviPort;
349 				gInfo->head_mode |= HEAD_MODE_B_DIGITAL;
350 			} else
351 				delete dviPort;
352 		}
353 	}
354 
355 	// then finally always try the analog port
356 	TRACE("Probing Analog\n");
357 	Port* analogPort = new(std::nothrow) AnalogPort();
358 	if (analogPort == NULL)
359 		return B_NO_MEMORY;
360 	if (analogPort->IsConnected()) {
361 		gInfo->ports[gInfo->port_count++] = analogPort;
362 		gInfo->head_mode |= HEAD_MODE_A_ANALOG;
363 	} else
364 		delete analogPort;
365 
366 	if (gInfo->port_count == 0)
367 		return B_ERROR;
368 
369 	// Activate reference clocks if needed
370 	if (gInfo->shared_info->pch_info == INTEL_PCH_IBX
371 		|| gInfo->shared_info->pch_info == INTEL_PCH_CPT) {
372 		TRACE("Activating clocks\n");
373 		refclk_activate_ilk(foundLVDS || foundDP || foundDDI);
374 	}
375 	/*
376 	} else if (gInfo->shared_info->pch_info == INTEL_PCH_LPT) {
377 		// TODO: Some kind of stepped bend thing?
378 		// only needed for vga
379 		refclk_activate_lpt(foundLVDS);
380 	}
381 	*/
382 
383 	TRACE("Probing complete.\n");
384 	return B_OK;
385 }
386 
387 
388 static status_t
389 assign_pipes()
390 {
391 	// TODO: At some point we should "group" ports to pipes with the same mode.
392 	// You can drive multiple ports from a single pipe as long as the mode is
393 	// the same. For the moment we could get displays with the wrong pipes
394 	// assigned when the count is > 1;
395 
396 	uint32 current = 0;
397 
398 	bool assigned[gInfo->pipe_count];
399 	memset(assigned, 0, gInfo->pipe_count);
400 
401 	// Some ports need to be assigned to a fixed pipe on old hardware (or due
402 	// to limitations in the current driver on current hardware). Assign those
403 	// first
404 	for (uint32 i = 0; i < gInfo->port_count; i++) {
405 		if (!gInfo->ports[i]->IsConnected())
406 			continue;
407 
408 		pipe_index preference = gInfo->ports[i]->PipePreference();
409 		if (preference != INTEL_PIPE_ANY) {
410 			int index = (preference == INTEL_PIPE_B) ? 1 : 0;
411 			if (assigned[index]) {
412 				TRACE("Pipe %d is already assigned, it will drive multiple "
413 					"displays\n", index);
414 			}
415 			gInfo->ports[i]->SetPipe(gInfo->pipes[index]);
416 			assigned[index] = true;
417 			continue;
418 		}
419 	}
420 
421 	// In a second pass, assign the remaining ports to the remaining pipes
422 	for (uint32 i = 0; i < gInfo->port_count; i++) {
423 		if (gInfo->ports[i]->IsConnected() && gInfo->ports[i]->GetPipe() == NULL) {
424 			while (current < gInfo->pipe_count && assigned[current])
425 				current++;
426 
427 			if (current >= gInfo->pipe_count) {
428 				ERROR("%s: No pipes left to assign to port %s!\n", __func__,
429 					gInfo->ports[i]->PortName());
430 				continue;
431 			}
432 
433 			gInfo->ports[i]->SetPipe(gInfo->pipes[current]);
434 			assigned[current] = true;
435 		}
436 	}
437 
438 	return B_OK;
439 }
440 
441 
442 //	#pragma mark - public accelerant functions
443 
444 
445 /*! Init primary accelerant */
446 status_t
447 intel_init_accelerant(int device)
448 {
449 	CALLED();
450 
451 	status_t status = init_common(device, false);
452 	if (status != B_OK)
453 		return status;
454 
455 	intel_shared_info &info = *gInfo->shared_info;
456 
457 	init_lock(&info.accelerant_lock, "intel extreme accelerant");
458 	init_lock(&info.engine_lock, "intel extreme engine");
459 
460 	setup_ring_buffer(info.primary_ring_buffer, "intel primary ring buffer");
461 
462 	// Probe all ports
463 	status = probe_ports();
464 
465 	// On TRACE, dump ports and states
466 	dump_ports();
467 
468 	if (status != B_OK)
469 		ERROR("Warning: zero active displays were found!\n");
470 
471 	status = assign_pipes();
472 
473 	if (status != B_OK)
474 		ERROR("Warning: error while assigning pipes!\n");
475 
476 	status = create_mode_list();
477 	if (status != B_OK) {
478 		uninit_common();
479 		return status;
480 	}
481 
482 	return B_OK;
483 }
484 
485 
486 ssize_t
487 intel_accelerant_clone_info_size(void)
488 {
489 	CALLED();
490 	// clone info is device name, so return its maximum size
491 	return B_PATH_NAME_LENGTH;
492 }
493 
494 
495 void
496 intel_get_accelerant_clone_info(void* info)
497 {
498 	CALLED();
499 	ioctl(gInfo->device, INTEL_GET_DEVICE_NAME, info, B_PATH_NAME_LENGTH);
500 }
501 
502 
503 status_t
504 intel_clone_accelerant(void* info)
505 {
506 	CALLED();
507 
508 	// create full device name
509 	char path[B_PATH_NAME_LENGTH];
510 	strcpy(path, "/dev/");
511 #ifdef __HAIKU__
512 	strlcat(path, (const char*)info, sizeof(path));
513 #else
514 	strcat(path, (const char*)info);
515 #endif
516 
517 	int fd = open(path, B_READ_WRITE);
518 	if (fd < 0)
519 		return errno;
520 
521 	status_t status = init_common(fd, true);
522 	if (status != B_OK)
523 		goto err1;
524 
525 	// get read-only clone of supported display modes
526 	status = gInfo->mode_list_area = clone_area(
527 		"intel extreme cloned modes", (void**)&gInfo->mode_list,
528 		B_ANY_ADDRESS, B_READ_AREA, gInfo->shared_info->mode_list_area);
529 	if (status < B_OK)
530 		goto err2;
531 
532 	return B_OK;
533 
534 err2:
535 	uninit_common();
536 err1:
537 	close(fd);
538 	return status;
539 }
540 
541 
542 /*! This function is called for both, the primary accelerant and all of
543 	its clones.
544 */
545 void
546 intel_uninit_accelerant(void)
547 {
548 	CALLED();
549 
550 	// delete accelerant instance data
551 	delete_area(gInfo->mode_list_area);
552 	gInfo->mode_list = NULL;
553 
554 	if (!gInfo->is_clone) {
555 		intel_shared_info &info = *gInfo->shared_info;
556 		uninit_lock(&info.accelerant_lock);
557 		uninit_lock(&info.engine_lock);
558 		uninit_ring_buffer(info.primary_ring_buffer);
559 	}
560 	uninit_common();
561 }
562 
563 
564 status_t
565 intel_get_accelerant_device_info(accelerant_device_info* info)
566 {
567 	CALLED();
568 
569 	info->version = B_ACCELERANT_VERSION;
570 
571 	DeviceType* type = &gInfo->shared_info->device_type;
572 
573 	if (type->InFamily(INTEL_FAMILY_8xx))
574 		strcpy(info->name, "Intel Extreme");
575 	else if (type->InFamily(INTEL_FAMILY_9xx))
576 		strcpy(info->name, "Intel GMA");
577 	else if (type->InFamily(INTEL_FAMILY_SOC0))
578 		strcpy(info->name, "Intel Atom");
579 	else if (type->InFamily(INTEL_FAMILY_SER5))
580 		strcpy(info->name, "Intel HD/Iris");
581 	else
582 		strcpy(info->name, "Intel");
583 
584 	strcpy(info->chipset, gInfo->shared_info->device_identifier);
585 	strcpy(info->serial_no, "None");
586 
587 	info->memory = gInfo->shared_info->graphics_memory_size;
588 	info->dac_speed = gInfo->shared_info->pll_info.max_frequency;
589 
590 	return B_OK;
591 }
592 
593 
594 sem_id
595 intel_accelerant_retrace_semaphore()
596 {
597 	CALLED();
598 	return gInfo->shared_info->vblank_sem;
599 }
600