⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tm1stuff.c

📁 PNX系列设备驱动 PNX系列设备驱动
💻 C
📖 第 1 页 / 共 3 页
字号:
	IOTable.granularity = 0;
	IOTable.firstPrepared = 0;
	IOTable.lengthPrepared = 0;
	IOTable.mappingEntryCount = 2;
	IOTable.logicalMapping = logical_mapping;
	IOTable.physicalMapping = physical_mapping;
	IOTable.rangeInfo.range.base = (LogicalAddress)*pp_lin_mem;
	IOTable.rangeInfo.range.length = nbytes;

	err  = PrepareMemoryForIO(&IOTable);
	if (err)
	{
      printf("AllocateSharedHostMemory: PrepareMemoryForIO failed\n");
	  return false;
	}
	
    for (i = 0; i < SH_MEM_SLOTS; i++)  
    {
      if (ShMemList[i].ptr == NULL) 
      {
	    ShMemList[i].id = IOTable.preparationID;
	    ShMemList[i].ptr = *pp_lin_mem;
        break;
      }
    }
    if (i == SH_MEM_SLOTS) 
    {
      printf("AllocateSharedHostMemory: shared memory pointer list is full\n");
      return false;
    }

	*pl_ph_mem = (unsigned long)*physical_mapping;	
    
    return true;
}

/* COPIED FULLY FROM APPLE :-) */

void
InitEmber(RegEntryID theEntryID)
{
	OSErr		theErr;
	UInt16		data16;
	UInt32		addr;
	UInt32		*ember_base;
	UInt32		*foo;
	
	// first thing to do is to make sure that memory access is enabled
	// this is the PCI command register
	addr = 0x4;
	theErr = ExpMgrConfigReadWord(&theEntryID, (LogicalAddress)addr, &data16);
	// set the memory space access enable
	data16 |= 1<<1;		
	theErr = ExpMgrConfigWriteWord(&theEntryID, (LogicalAddress)addr, (UInt16)data16);
	
	theErr = GetBaseAddress( &theEntryID, (UInt32*)&ember_base, CONFIG_BASE0_ADDRESS, nil );
	foo = ember_base;
	foo += 0x20000 >> 2;
	*foo = 0x00004006;				// Send data out D1 and onto DAV

	theErr = ExpMgrConfigWriteWord(&theEntryID, (LogicalAddress)addr, 0x14);
}

void FindTMs()
{
  	const unsigned char philips[] = {0,0,17,49};
  	const unsigned char trimedia[] = {0,0,84,0};
  	
	RegEntryID rid;
	RegEntryIter cookie;
  	Boolean done, is_tm1;
  	OSStatus err = noErr;
   	int j;
 	void* ptr;
  	unsigned long sz;
  	
   	char buf [16]; 
 	char *vendor, *device, *slot, *name;
  	unsigned long frequency, mmio_base, sdram_base, sdram_size;

	if(GetTMPreference("frequency", buf)) 
	{
	  frequency =  (unsigned long) strtoul(buf, NULL, 10);
	} 
	else 
	{
	  printf("** Warning: Could not read TM1 frequency from preference file, assuming 80Mhz\n");
	  frequency = 80*1000*1000;
	}
   

	NumberOfTMs = 0;

	RegistryEntryIDInit(& rid);

	RegistryEntryIterateCreate(& cookie);

	while (1) 
	{
	  is_tm1 = true;
	  
	  /* take next entry from the name registry, quit loop if there is none*/
	  err = RegistryEntryIterate(&cookie, kRegIterContinue, &rid, &done);
	  if (done) 
	  {
	    break;
	  }  
	  
	  GetDeviceProperty(&rid, "name", &ptr, & sz);
	  name = (char*)ptr;
	  
	  /* check whether this is an 'Ember' (?) and initialise it */
	  if(strcmp(name, "pci10ee,4031")==0 || strcmp(name, "pci106b,16")==0) 
	  {
#ifdef DEBUG
	    printf("\t[Found an \"Ember\" (%s) ... initializing it.]\n", name);
#endif
	    InitEmber(rid);
	  }	
	  
	  /* check the vendor-id, and quit loop if it is not philips */
	  GetDeviceProperty(&rid, "vendor-id", &ptr, & sz); 
	  vendor = (char*) ptr;
	  for (j =0; j< sz && j < 32; j++) 
	  {
	    if (vendor[j] != philips[j]) 
	    {
	      is_tm1 = false;
	    }
	  }
	  if (!is_tm1) 
	  {
	    continue;
	  }

	  /* check the device-id, and quit loop if it is not TriMedia */
	  GetDeviceProperty(&rid, "device-id", &ptr, & sz); 
	  device = (char*) ptr;
	  for (j =0; j< sz; j++) 
	  {
	    if (device[j] != trimedia[j]) 
	    { 
	      is_tm1 = false;
	    }
	  }
	  if (!is_tm1) 
	  {
	    continue;
	  }

	  /* if the array is full give a warning, and continue search */
	  if (NumberOfTMs >= MAX_TM) 
	  {
	    printf("Warning: Cannot store info on %s\n", name);
	    continue;
	  }
	  
	  /* get mmio and sdram locations */
	  GetBaseAddress( &rid, &mmio_base, CONFIG_BASE1_ADDRESS, nil );
	  GetBaseAddress( &rid, &sdram_base, CONFIG_BASE0_ADDRESS, &sdram_size );
	  GetRevisionID( &rid, &TMInfo[NumberOfTMs].revision_id);

	  /* get slot name */
	  err = GetDeviceProperty(&rid, "AAPL,slot-name", &ptr, & sz);
	  if (err == noErr) 
	  {
	    slot = (char*)ptr;	
	  } 
	  else 
	  {
	    continue;
	  }
	    
      /* save the gathered info in the array */
      TMInfo[NumberOfTMs].NodeID     = rid;
      strncpy(TMInfo[NumberOfTMs].node_name, name, 16) ;
      strncpy(TMInfo[NumberOfTMs].slot, slot, 4) ;
      TMInfo[NumberOfTMs].sdram_base  = sdram_base;
      TMInfo[NumberOfTMs].sdram_size  = sdram_size;
      TMInfo[NumberOfTMs].mmio_base   = mmio_base;      
      TMInfo[NumberOfTMs].frequency   = frequency;      
     // TMInfo[NumberOfTMs].initialized = false;      
	  _MMIO_bases[NumberOfTMs] = (volatile unsigned long *) mmio_base;
      
      NumberOfTMs++;
  	}
    
  	RegistryEntryIterateDispose(& cookie);
  	RegistryEntryIDDispose(&rid); 
  
  	if (NumberOfTMs == 0) 
  	{
  	  return ;
  	}
  	if ( AllocateSharedHostMemory(32, &_HostCall_commvar, &HostCall_commvar_target) )
    {
#ifdef DEBUG
    printf("_HostCall_commvar: 0x%x\n", _HostCall_commvar);
    printf("HostCall_commvar_target: 0x%x\n", HostCall_commvar_target);
#endif    
  	} 
  	else 
  	{
      NumberOfTMs = -1;
    }
}

Boolean FreeSharedHostMemory(void* shm_ptr) 
{
    int i;
    
	if (shm_ptr) 
	{
      for (i = 0; i < SH_MEM_SLOTS; i++)  
      {
    	if (ShMemList[i].ptr == shm_ptr) 
    	{
		  CheckpointIO(ShMemList[i].id, kNilOptions);
		  MemDeallocatePhysicallyContiguous(ShMemList[i].ptr);
	      ShMemList[i].ptr = NULL;
		  return true;
        }
      }
	}
	
	printf("TM1Quit: invalid pointer");
    return false;
}


//===================================================================

Boolean 
LoadTM(UInt number_of_nodes, Int32 node, char *filename,
   TMDwnLdr_SharedSectionTab_Handle shared_sections, int argc, char *argv[])
{
	TMDwnLdr_Object_Handle handle;
	TMDwnLdr_Status status;
	UInt32      sdram_base;
	UInt32      mmio_base;
	UInt32      sdram_len;
	UInt32      TM_freq;
	int         exit;

	TMInfo[node].TM_running = false;
	TM_freq = TMInfo[node].frequency;
	sdram_base = MMIO_M(node, DRAM_BASE);
	mmio_base = MMIO_M(node, MMIO_BASE);

	sdram_len = MMIO_M(node, DRAM_LIMIT) - MMIO_M(node, DRAM_BASE);

#ifdef DEBUG
	printf("\n****\nnumber of nodes: %d\n", number_of_nodes);
	printf("node: %d\n", node);
	printf("filename: %s\n", filename);
	printf("argc: %d\nargv: ", argc);
	for (i = 0; i < argc; i++)
		printf("%s ", argv[i]);
	printf("\n");
	printf("_HostCall_commvar: 0x%x\n", _HostCall_commvar);
	printf("HostCall_commvar_target: 0x%x\n", HostCall_commvar_target);
	printf("_MMIO_bases[%d]: 0x%x\n", node, _MMIO_bases[node]);
	printf("sdram_base: 0x%x\n", sdram_base);
	printf("mmio_base: 0x%x\n", mmio_base);
	printf("sdram_len: 0x%x\n", sdram_len);
	printf("TM_freq: %d\n", TM_freq);
#endif

	MMIO_M(node, BIU_CTL) |= SET_RST_MASK;
	MMIO_M(node, BIU_CTL) &= ~SET_RST_MASK;

	status = TMDwnLdr_load_object_from_file(filename, shared_sections, &handle);
	if (status != TMDwnLdr_OK) {
		printf("** File '%s' could not be read as an executable\n", filename);
		printf("   [%s]\n", TMDwnLdr_get_last_error(status));
		return (false);
	}

	status = TMDwnLdr_resolve_symbol(handle, "__HostCall_commvar_init", (UInt) HostCall_commvar_target);
	if (status != TMDwnLdr_OK) 
	{
		printf("** Warning: Could not resolve '_HostCall_commvar_init' symbol in file %s\n", filename);
		printf("**          No host communication will be available.\n");		
		printf("**          To stop program execution after 'g[o]' command enter Command-Period.\n");		
#if 0
		printf("** Could not resolve symbol in file %s\n", filename);
		printf("   [%s]\n", TMDwnLdr_get_last_error(status));
		TMDwnLdr_unload_object(handle);
		return (false);
#endif
	}

	status = TMDwnLdr_multiproc_relocate(handle, tmMacOSHost, (Address *) _MMIO_bases,
					     (UInt) node,
					     number_of_nodes,	/* Number of
								 * executables. */
					     (UInt) TM_freq,
					     (Address) sdram_base,
					     (UInt) sdram_len,
					 TMDwnLdr_LeaveCachingToDownloader);

	if (status != TMDwnLdr_OK) {
		printf("** Could not multiproc relocate in file %s\n", filename);
		printf("   [%s]\n", TMDwnLdr_get_last_error(status));
		TMDwnLdr_unload_object(handle);
		return (false);
	}

	status = TMDwnLdr_get_memory_image(handle, (Address) MMIO_M(node, DRAM_BASE));
	if (status != TMDwnLdr_OK) {
		printf("** Could not get memory image in file %s\n", filename);
		printf("   [%s]\n", TMDwnLdr_get_last_error(status));
		TMDwnLdr_unload_object(handle);
		return (false);
	}
        TMDwnLdr_get_endian(handle,&object_endian);
	TMDwnLdr_unload_object(handle);

	if (node == 0) {
		TM1IF_init(
			   (RPCServ_OpenFunc) open,
			   (RPCServ_OpenDllFunc) my_open_dll,
			   (RPCServ_CloseFunc) close,
			   (RPCServ_ReadFunc) my_read,
			   (RPCServ_WriteFunc) my_write,
			   (RPCServ_SeekFunc) lseek,
			   (RPCServ_IsattyFunc) isatty,
			   (RPCServ_FstatFunc) my_fstat,
			   (RPCServ_FcntlFunc) fcntl,
			   (RPCServ_StatFunc) my_stat,
			   (RPCServ_ExitFunc) exitf,
			   object_endian);
	}

	/* 
		Open any files to be redirected. Delete any existing stdout or stderr files
		of the same name.
	*/

	if (stdin_fd != STDIN_HANDLE) 
		close (stdin_fd);
	stdin_fd = STDIN_HANDLE;
	if (stdin_file[0])
	{
		stdin_fd = open(stdin_file, O_RDONLY);
		if (stdin_fd < 0)
		{
			printf("** Cannot not open redirected stdin file '%s'\n", stdin_file);
			stdin_fd = STDIN_HANDLE;
		}	
	}

	if (stdout_fd != STDOUT_HANDLE) 
		close (stdout_fd);
	stdout_fd = STDOUT_HANDLE;
	if (stdout_file[0])
	{
		remove(stdout_file);
		stdout_fd = open(stdout_file, O_CREAT);
		if (stdout_fd < 0)
		{
			printf("** Cannot not open redirected stdout file '%s'\n", stdout_file);
			stdout_fd = STDOUT_HANDLE;
		}	
	}

	if (stderr_fd != STDERR_HANDLE) 
		close (stderr_fd);
	stderr_fd = STDERR_HANDLE;	
	if (stderr_file[0])
	{
		remove(stderr_file);
		stderr_fd = open(stderr_file, O_CREAT);
		if (stderr_fd < 0)
		{
			printf("** Cannot not open redirected stderr file '%s'\n", stderr_file);
			stderr_fd = STDERR_HANDLE;
		}	
	}
	
	TM1IF_add_node_info(node, argc, argv, 
                       stdin_fd, stdout_fd, stderr_fd, 0,
                       MMIO_M(node, DRAM_BASE), MMIO_M(node, DRAM_LIMIT),
                       Null
                      );

	return (true);
}


//===================================================================

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -