i915_gem.c 137 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332
  1. /*
  2. * Copyright © 2008-2015 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 DEALINGS
  21. * IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Eric Anholt <eric@anholt.net>
  25. *
  26. */
  27. #include <drm/drmP.h>
  28. #include <drm/drm_vma_manager.h>
  29. #include <drm/i915_drm.h>
  30. #include "i915_drv.h"
  31. #include "i915_vgpu.h"
  32. #include "i915_trace.h"
  33. #include "intel_drv.h"
  34. #include "intel_mocs.h"
  35. #include <linux/shmem_fs.h>
  36. #include <linux/slab.h>
  37. #include <linux/swap.h>
  38. #include <linux/pci.h>
  39. #include <linux/dma-buf.h>
  40. static void i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj);
  41. static void i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj);
  42. static void
  43. i915_gem_object_retire__write(struct drm_i915_gem_object *obj);
  44. static void
  45. i915_gem_object_retire__read(struct drm_i915_gem_object *obj, int ring);
  46. static bool cpu_cache_is_coherent(struct drm_device *dev,
  47. enum i915_cache_level level)
  48. {
  49. return HAS_LLC(dev) || level != I915_CACHE_NONE;
  50. }
  51. static bool cpu_write_needs_clflush(struct drm_i915_gem_object *obj)
  52. {
  53. if (!cpu_cache_is_coherent(obj->base.dev, obj->cache_level))
  54. return true;
  55. return obj->pin_display;
  56. }
  57. /* some bookkeeping */
  58. static void i915_gem_info_add_obj(struct drm_i915_private *dev_priv,
  59. size_t size)
  60. {
  61. spin_lock(&dev_priv->mm.object_stat_lock);
  62. dev_priv->mm.object_count++;
  63. dev_priv->mm.object_memory += size;
  64. spin_unlock(&dev_priv->mm.object_stat_lock);
  65. }
  66. static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv,
  67. size_t size)
  68. {
  69. spin_lock(&dev_priv->mm.object_stat_lock);
  70. dev_priv->mm.object_count--;
  71. dev_priv->mm.object_memory -= size;
  72. spin_unlock(&dev_priv->mm.object_stat_lock);
  73. }
  74. static int
  75. i915_gem_wait_for_error(struct i915_gpu_error *error)
  76. {
  77. int ret;
  78. if (!i915_reset_in_progress(error))
  79. return 0;
  80. /*
  81. * Only wait 10 seconds for the gpu reset to complete to avoid hanging
  82. * userspace. If it takes that long something really bad is going on and
  83. * we should simply try to bail out and fail as gracefully as possible.
  84. */
  85. ret = wait_event_interruptible_timeout(error->reset_queue,
  86. !i915_reset_in_progress(error),
  87. 10*HZ);
  88. if (ret == 0) {
  89. DRM_ERROR("Timed out waiting for the gpu reset to complete\n");
  90. return -EIO;
  91. } else if (ret < 0) {
  92. return ret;
  93. } else {
  94. return 0;
  95. }
  96. }
  97. int i915_mutex_lock_interruptible(struct drm_device *dev)
  98. {
  99. struct drm_i915_private *dev_priv = dev->dev_private;
  100. int ret;
  101. ret = i915_gem_wait_for_error(&dev_priv->gpu_error);
  102. if (ret)
  103. return ret;
  104. ret = mutex_lock_interruptible(&dev->struct_mutex);
  105. if (ret)
  106. return ret;
  107. WARN_ON(i915_verify_lists(dev));
  108. return 0;
  109. }
  110. int
  111. i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
  112. struct drm_file *file)
  113. {
  114. struct drm_i915_private *dev_priv = to_i915(dev);
  115. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  116. struct drm_i915_gem_get_aperture *args = data;
  117. struct i915_vma *vma;
  118. size_t pinned;
  119. pinned = 0;
  120. mutex_lock(&dev->struct_mutex);
  121. list_for_each_entry(vma, &ggtt->base.active_list, vm_link)
  122. if (vma->pin_count)
  123. pinned += vma->node.size;
  124. list_for_each_entry(vma, &ggtt->base.inactive_list, vm_link)
  125. if (vma->pin_count)
  126. pinned += vma->node.size;
  127. mutex_unlock(&dev->struct_mutex);
  128. args->aper_size = ggtt->base.total;
  129. args->aper_available_size = args->aper_size - pinned;
  130. return 0;
  131. }
  132. static int
  133. i915_gem_object_get_pages_phys(struct drm_i915_gem_object *obj)
  134. {
  135. struct address_space *mapping = file_inode(obj->base.filp)->i_mapping;
  136. char *vaddr = obj->phys_handle->vaddr;
  137. struct sg_table *st;
  138. struct scatterlist *sg;
  139. int i;
  140. if (WARN_ON(i915_gem_object_needs_bit17_swizzle(obj)))
  141. return -EINVAL;
  142. for (i = 0; i < obj->base.size / PAGE_SIZE; i++) {
  143. struct page *page;
  144. char *src;
  145. page = shmem_read_mapping_page(mapping, i);
  146. if (IS_ERR(page))
  147. return PTR_ERR(page);
  148. src = kmap_atomic(page);
  149. memcpy(vaddr, src, PAGE_SIZE);
  150. drm_clflush_virt_range(vaddr, PAGE_SIZE);
  151. kunmap_atomic(src);
  152. put_page(page);
  153. vaddr += PAGE_SIZE;
  154. }
  155. i915_gem_chipset_flush(to_i915(obj->base.dev));
  156. st = kmalloc(sizeof(*st), GFP_KERNEL);
  157. if (st == NULL)
  158. return -ENOMEM;
  159. if (sg_alloc_table(st, 1, GFP_KERNEL)) {
  160. kfree(st);
  161. return -ENOMEM;
  162. }
  163. sg = st->sgl;
  164. sg->offset = 0;
  165. sg->length = obj->base.size;
  166. sg_dma_address(sg) = obj->phys_handle->busaddr;
  167. sg_dma_len(sg) = obj->base.size;
  168. obj->pages = st;
  169. return 0;
  170. }
  171. static void
  172. i915_gem_object_put_pages_phys(struct drm_i915_gem_object *obj)
  173. {
  174. int ret;
  175. BUG_ON(obj->madv == __I915_MADV_PURGED);
  176. ret = i915_gem_object_set_to_cpu_domain(obj, true);
  177. if (WARN_ON(ret)) {
  178. /* In the event of a disaster, abandon all caches and
  179. * hope for the best.
  180. */
  181. obj->base.read_domains = obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  182. }
  183. if (obj->madv == I915_MADV_DONTNEED)
  184. obj->dirty = 0;
  185. if (obj->dirty) {
  186. struct address_space *mapping = file_inode(obj->base.filp)->i_mapping;
  187. char *vaddr = obj->phys_handle->vaddr;
  188. int i;
  189. for (i = 0; i < obj->base.size / PAGE_SIZE; i++) {
  190. struct page *page;
  191. char *dst;
  192. page = shmem_read_mapping_page(mapping, i);
  193. if (IS_ERR(page))
  194. continue;
  195. dst = kmap_atomic(page);
  196. drm_clflush_virt_range(vaddr, PAGE_SIZE);
  197. memcpy(dst, vaddr, PAGE_SIZE);
  198. kunmap_atomic(dst);
  199. set_page_dirty(page);
  200. if (obj->madv == I915_MADV_WILLNEED)
  201. mark_page_accessed(page);
  202. put_page(page);
  203. vaddr += PAGE_SIZE;
  204. }
  205. obj->dirty = 0;
  206. }
  207. sg_free_table(obj->pages);
  208. kfree(obj->pages);
  209. }
  210. static void
  211. i915_gem_object_release_phys(struct drm_i915_gem_object *obj)
  212. {
  213. drm_pci_free(obj->base.dev, obj->phys_handle);
  214. }
  215. static const struct drm_i915_gem_object_ops i915_gem_phys_ops = {
  216. .get_pages = i915_gem_object_get_pages_phys,
  217. .put_pages = i915_gem_object_put_pages_phys,
  218. .release = i915_gem_object_release_phys,
  219. };
  220. static int
  221. drop_pages(struct drm_i915_gem_object *obj)
  222. {
  223. struct i915_vma *vma, *next;
  224. int ret;
  225. drm_gem_object_reference(&obj->base);
  226. list_for_each_entry_safe(vma, next, &obj->vma_list, obj_link)
  227. if (i915_vma_unbind(vma))
  228. break;
  229. ret = i915_gem_object_put_pages(obj);
  230. drm_gem_object_unreference(&obj->base);
  231. return ret;
  232. }
  233. int
  234. i915_gem_object_attach_phys(struct drm_i915_gem_object *obj,
  235. int align)
  236. {
  237. drm_dma_handle_t *phys;
  238. int ret;
  239. if (obj->phys_handle) {
  240. if ((unsigned long)obj->phys_handle->vaddr & (align -1))
  241. return -EBUSY;
  242. return 0;
  243. }
  244. if (obj->madv != I915_MADV_WILLNEED)
  245. return -EFAULT;
  246. if (obj->base.filp == NULL)
  247. return -EINVAL;
  248. ret = drop_pages(obj);
  249. if (ret)
  250. return ret;
  251. /* create a new object */
  252. phys = drm_pci_alloc(obj->base.dev, obj->base.size, align);
  253. if (!phys)
  254. return -ENOMEM;
  255. obj->phys_handle = phys;
  256. obj->ops = &i915_gem_phys_ops;
  257. return i915_gem_object_get_pages(obj);
  258. }
  259. static int
  260. i915_gem_phys_pwrite(struct drm_i915_gem_object *obj,
  261. struct drm_i915_gem_pwrite *args,
  262. struct drm_file *file_priv)
  263. {
  264. struct drm_device *dev = obj->base.dev;
  265. void *vaddr = obj->phys_handle->vaddr + args->offset;
  266. char __user *user_data = to_user_ptr(args->data_ptr);
  267. int ret = 0;
  268. /* We manually control the domain here and pretend that it
  269. * remains coherent i.e. in the GTT domain, like shmem_pwrite.
  270. */
  271. ret = i915_gem_object_wait_rendering(obj, false);
  272. if (ret)
  273. return ret;
  274. intel_fb_obj_invalidate(obj, ORIGIN_CPU);
  275. if (__copy_from_user_inatomic_nocache(vaddr, user_data, args->size)) {
  276. unsigned long unwritten;
  277. /* The physical object once assigned is fixed for the lifetime
  278. * of the obj, so we can safely drop the lock and continue
  279. * to access vaddr.
  280. */
  281. mutex_unlock(&dev->struct_mutex);
  282. unwritten = copy_from_user(vaddr, user_data, args->size);
  283. mutex_lock(&dev->struct_mutex);
  284. if (unwritten) {
  285. ret = -EFAULT;
  286. goto out;
  287. }
  288. }
  289. drm_clflush_virt_range(vaddr, args->size);
  290. i915_gem_chipset_flush(to_i915(dev));
  291. out:
  292. intel_fb_obj_flush(obj, false, ORIGIN_CPU);
  293. return ret;
  294. }
  295. void *i915_gem_object_alloc(struct drm_device *dev)
  296. {
  297. struct drm_i915_private *dev_priv = dev->dev_private;
  298. return kmem_cache_zalloc(dev_priv->objects, GFP_KERNEL);
  299. }
  300. void i915_gem_object_free(struct drm_i915_gem_object *obj)
  301. {
  302. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  303. kmem_cache_free(dev_priv->objects, obj);
  304. }
  305. static int
  306. i915_gem_create(struct drm_file *file,
  307. struct drm_device *dev,
  308. uint64_t size,
  309. uint32_t *handle_p)
  310. {
  311. struct drm_i915_gem_object *obj;
  312. int ret;
  313. u32 handle;
  314. size = roundup(size, PAGE_SIZE);
  315. if (size == 0)
  316. return -EINVAL;
  317. /* Allocate the new object */
  318. obj = i915_gem_object_create(dev, size);
  319. if (IS_ERR(obj))
  320. return PTR_ERR(obj);
  321. ret = drm_gem_handle_create(file, &obj->base, &handle);
  322. /* drop reference from allocate - handle holds it now */
  323. drm_gem_object_unreference_unlocked(&obj->base);
  324. if (ret)
  325. return ret;
  326. *handle_p = handle;
  327. return 0;
  328. }
  329. int
  330. i915_gem_dumb_create(struct drm_file *file,
  331. struct drm_device *dev,
  332. struct drm_mode_create_dumb *args)
  333. {
  334. /* have to work out size/pitch and return them */
  335. args->pitch = ALIGN(args->width * DIV_ROUND_UP(args->bpp, 8), 64);
  336. args->size = args->pitch * args->height;
  337. return i915_gem_create(file, dev,
  338. args->size, &args->handle);
  339. }
  340. /**
  341. * Creates a new mm object and returns a handle to it.
  342. */
  343. int
  344. i915_gem_create_ioctl(struct drm_device *dev, void *data,
  345. struct drm_file *file)
  346. {
  347. struct drm_i915_gem_create *args = data;
  348. return i915_gem_create(file, dev,
  349. args->size, &args->handle);
  350. }
  351. static inline int
  352. __copy_to_user_swizzled(char __user *cpu_vaddr,
  353. const char *gpu_vaddr, int gpu_offset,
  354. int length)
  355. {
  356. int ret, cpu_offset = 0;
  357. while (length > 0) {
  358. int cacheline_end = ALIGN(gpu_offset + 1, 64);
  359. int this_length = min(cacheline_end - gpu_offset, length);
  360. int swizzled_gpu_offset = gpu_offset ^ 64;
  361. ret = __copy_to_user(cpu_vaddr + cpu_offset,
  362. gpu_vaddr + swizzled_gpu_offset,
  363. this_length);
  364. if (ret)
  365. return ret + length;
  366. cpu_offset += this_length;
  367. gpu_offset += this_length;
  368. length -= this_length;
  369. }
  370. return 0;
  371. }
  372. static inline int
  373. __copy_from_user_swizzled(char *gpu_vaddr, int gpu_offset,
  374. const char __user *cpu_vaddr,
  375. int length)
  376. {
  377. int ret, cpu_offset = 0;
  378. while (length > 0) {
  379. int cacheline_end = ALIGN(gpu_offset + 1, 64);
  380. int this_length = min(cacheline_end - gpu_offset, length);
  381. int swizzled_gpu_offset = gpu_offset ^ 64;
  382. ret = __copy_from_user(gpu_vaddr + swizzled_gpu_offset,
  383. cpu_vaddr + cpu_offset,
  384. this_length);
  385. if (ret)
  386. return ret + length;
  387. cpu_offset += this_length;
  388. gpu_offset += this_length;
  389. length -= this_length;
  390. }
  391. return 0;
  392. }
  393. /*
  394. * Pins the specified object's pages and synchronizes the object with
  395. * GPU accesses. Sets needs_clflush to non-zero if the caller should
  396. * flush the object from the CPU cache.
  397. */
  398. int i915_gem_obj_prepare_shmem_read(struct drm_i915_gem_object *obj,
  399. int *needs_clflush)
  400. {
  401. int ret;
  402. *needs_clflush = 0;
  403. if (WARN_ON((obj->ops->flags & I915_GEM_OBJECT_HAS_STRUCT_PAGE) == 0))
  404. return -EINVAL;
  405. if (!(obj->base.read_domains & I915_GEM_DOMAIN_CPU)) {
  406. /* If we're not in the cpu read domain, set ourself into the gtt
  407. * read domain and manually flush cachelines (if required). This
  408. * optimizes for the case when the gpu will dirty the data
  409. * anyway again before the next pread happens. */
  410. *needs_clflush = !cpu_cache_is_coherent(obj->base.dev,
  411. obj->cache_level);
  412. ret = i915_gem_object_wait_rendering(obj, true);
  413. if (ret)
  414. return ret;
  415. }
  416. ret = i915_gem_object_get_pages(obj);
  417. if (ret)
  418. return ret;
  419. i915_gem_object_pin_pages(obj);
  420. return ret;
  421. }
  422. /* Per-page copy function for the shmem pread fastpath.
  423. * Flushes invalid cachelines before reading the target if
  424. * needs_clflush is set. */
  425. static int
  426. shmem_pread_fast(struct page *page, int shmem_page_offset, int page_length,
  427. char __user *user_data,
  428. bool page_do_bit17_swizzling, bool needs_clflush)
  429. {
  430. char *vaddr;
  431. int ret;
  432. if (unlikely(page_do_bit17_swizzling))
  433. return -EINVAL;
  434. vaddr = kmap_atomic(page);
  435. if (needs_clflush)
  436. drm_clflush_virt_range(vaddr + shmem_page_offset,
  437. page_length);
  438. ret = __copy_to_user_inatomic(user_data,
  439. vaddr + shmem_page_offset,
  440. page_length);
  441. kunmap_atomic(vaddr);
  442. return ret ? -EFAULT : 0;
  443. }
  444. static void
  445. shmem_clflush_swizzled_range(char *addr, unsigned long length,
  446. bool swizzled)
  447. {
  448. if (unlikely(swizzled)) {
  449. unsigned long start = (unsigned long) addr;
  450. unsigned long end = (unsigned long) addr + length;
  451. /* For swizzling simply ensure that we always flush both
  452. * channels. Lame, but simple and it works. Swizzled
  453. * pwrite/pread is far from a hotpath - current userspace
  454. * doesn't use it at all. */
  455. start = round_down(start, 128);
  456. end = round_up(end, 128);
  457. drm_clflush_virt_range((void *)start, end - start);
  458. } else {
  459. drm_clflush_virt_range(addr, length);
  460. }
  461. }
  462. /* Only difference to the fast-path function is that this can handle bit17
  463. * and uses non-atomic copy and kmap functions. */
  464. static int
  465. shmem_pread_slow(struct page *page, int shmem_page_offset, int page_length,
  466. char __user *user_data,
  467. bool page_do_bit17_swizzling, bool needs_clflush)
  468. {
  469. char *vaddr;
  470. int ret;
  471. vaddr = kmap(page);
  472. if (needs_clflush)
  473. shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
  474. page_length,
  475. page_do_bit17_swizzling);
  476. if (page_do_bit17_swizzling)
  477. ret = __copy_to_user_swizzled(user_data,
  478. vaddr, shmem_page_offset,
  479. page_length);
  480. else
  481. ret = __copy_to_user(user_data,
  482. vaddr + shmem_page_offset,
  483. page_length);
  484. kunmap(page);
  485. return ret ? - EFAULT : 0;
  486. }
  487. static int
  488. i915_gem_shmem_pread(struct drm_device *dev,
  489. struct drm_i915_gem_object *obj,
  490. struct drm_i915_gem_pread *args,
  491. struct drm_file *file)
  492. {
  493. char __user *user_data;
  494. ssize_t remain;
  495. loff_t offset;
  496. int shmem_page_offset, page_length, ret = 0;
  497. int obj_do_bit17_swizzling, page_do_bit17_swizzling;
  498. int prefaulted = 0;
  499. int needs_clflush = 0;
  500. struct sg_page_iter sg_iter;
  501. user_data = to_user_ptr(args->data_ptr);
  502. remain = args->size;
  503. obj_do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
  504. ret = i915_gem_obj_prepare_shmem_read(obj, &needs_clflush);
  505. if (ret)
  506. return ret;
  507. offset = args->offset;
  508. for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents,
  509. offset >> PAGE_SHIFT) {
  510. struct page *page = sg_page_iter_page(&sg_iter);
  511. if (remain <= 0)
  512. break;
  513. /* Operation in this page
  514. *
  515. * shmem_page_offset = offset within page in shmem file
  516. * page_length = bytes to copy for this page
  517. */
  518. shmem_page_offset = offset_in_page(offset);
  519. page_length = remain;
  520. if ((shmem_page_offset + page_length) > PAGE_SIZE)
  521. page_length = PAGE_SIZE - shmem_page_offset;
  522. page_do_bit17_swizzling = obj_do_bit17_swizzling &&
  523. (page_to_phys(page) & (1 << 17)) != 0;
  524. ret = shmem_pread_fast(page, shmem_page_offset, page_length,
  525. user_data, page_do_bit17_swizzling,
  526. needs_clflush);
  527. if (ret == 0)
  528. goto next_page;
  529. mutex_unlock(&dev->struct_mutex);
  530. if (likely(!i915.prefault_disable) && !prefaulted) {
  531. ret = fault_in_multipages_writeable(user_data, remain);
  532. /* Userspace is tricking us, but we've already clobbered
  533. * its pages with the prefault and promised to write the
  534. * data up to the first fault. Hence ignore any errors
  535. * and just continue. */
  536. (void)ret;
  537. prefaulted = 1;
  538. }
  539. ret = shmem_pread_slow(page, shmem_page_offset, page_length,
  540. user_data, page_do_bit17_swizzling,
  541. needs_clflush);
  542. mutex_lock(&dev->struct_mutex);
  543. if (ret)
  544. goto out;
  545. next_page:
  546. remain -= page_length;
  547. user_data += page_length;
  548. offset += page_length;
  549. }
  550. out:
  551. i915_gem_object_unpin_pages(obj);
  552. return ret;
  553. }
  554. /**
  555. * Reads data from the object referenced by handle.
  556. *
  557. * On error, the contents of *data are undefined.
  558. */
  559. int
  560. i915_gem_pread_ioctl(struct drm_device *dev, void *data,
  561. struct drm_file *file)
  562. {
  563. struct drm_i915_gem_pread *args = data;
  564. struct drm_i915_gem_object *obj;
  565. int ret = 0;
  566. if (args->size == 0)
  567. return 0;
  568. if (!access_ok(VERIFY_WRITE,
  569. to_user_ptr(args->data_ptr),
  570. args->size))
  571. return -EFAULT;
  572. ret = i915_mutex_lock_interruptible(dev);
  573. if (ret)
  574. return ret;
  575. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  576. if (&obj->base == NULL) {
  577. ret = -ENOENT;
  578. goto unlock;
  579. }
  580. /* Bounds check source. */
  581. if (args->offset > obj->base.size ||
  582. args->size > obj->base.size - args->offset) {
  583. ret = -EINVAL;
  584. goto out;
  585. }
  586. /* prime objects have no backing filp to GEM pread/pwrite
  587. * pages from.
  588. */
  589. if (!obj->base.filp) {
  590. ret = -EINVAL;
  591. goto out;
  592. }
  593. trace_i915_gem_object_pread(obj, args->offset, args->size);
  594. ret = i915_gem_shmem_pread(dev, obj, args, file);
  595. out:
  596. drm_gem_object_unreference(&obj->base);
  597. unlock:
  598. mutex_unlock(&dev->struct_mutex);
  599. return ret;
  600. }
  601. /* This is the fast write path which cannot handle
  602. * page faults in the source data
  603. */
  604. static inline int
  605. fast_user_write(struct io_mapping *mapping,
  606. loff_t page_base, int page_offset,
  607. char __user *user_data,
  608. int length)
  609. {
  610. void __iomem *vaddr_atomic;
  611. void *vaddr;
  612. unsigned long unwritten;
  613. vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base);
  614. /* We can use the cpu mem copy function because this is X86. */
  615. vaddr = (void __force*)vaddr_atomic + page_offset;
  616. unwritten = __copy_from_user_inatomic_nocache(vaddr,
  617. user_data, length);
  618. io_mapping_unmap_atomic(vaddr_atomic);
  619. return unwritten;
  620. }
  621. /**
  622. * This is the fast pwrite path, where we copy the data directly from the
  623. * user into the GTT, uncached.
  624. */
  625. static int
  626. i915_gem_gtt_pwrite_fast(struct drm_device *dev,
  627. struct drm_i915_gem_object *obj,
  628. struct drm_i915_gem_pwrite *args,
  629. struct drm_file *file)
  630. {
  631. struct drm_i915_private *dev_priv = to_i915(dev);
  632. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  633. ssize_t remain;
  634. loff_t offset, page_base;
  635. char __user *user_data;
  636. int page_offset, page_length, ret;
  637. ret = i915_gem_obj_ggtt_pin(obj, 0, PIN_MAPPABLE | PIN_NONBLOCK);
  638. if (ret)
  639. goto out;
  640. ret = i915_gem_object_set_to_gtt_domain(obj, true);
  641. if (ret)
  642. goto out_unpin;
  643. ret = i915_gem_object_put_fence(obj);
  644. if (ret)
  645. goto out_unpin;
  646. user_data = to_user_ptr(args->data_ptr);
  647. remain = args->size;
  648. offset = i915_gem_obj_ggtt_offset(obj) + args->offset;
  649. intel_fb_obj_invalidate(obj, ORIGIN_GTT);
  650. while (remain > 0) {
  651. /* Operation in this page
  652. *
  653. * page_base = page offset within aperture
  654. * page_offset = offset within page
  655. * page_length = bytes to copy for this page
  656. */
  657. page_base = offset & PAGE_MASK;
  658. page_offset = offset_in_page(offset);
  659. page_length = remain;
  660. if ((page_offset + remain) > PAGE_SIZE)
  661. page_length = PAGE_SIZE - page_offset;
  662. /* If we get a fault while copying data, then (presumably) our
  663. * source page isn't available. Return the error and we'll
  664. * retry in the slow path.
  665. */
  666. if (fast_user_write(ggtt->mappable, page_base,
  667. page_offset, user_data, page_length)) {
  668. ret = -EFAULT;
  669. goto out_flush;
  670. }
  671. remain -= page_length;
  672. user_data += page_length;
  673. offset += page_length;
  674. }
  675. out_flush:
  676. intel_fb_obj_flush(obj, false, ORIGIN_GTT);
  677. out_unpin:
  678. i915_gem_object_ggtt_unpin(obj);
  679. out:
  680. return ret;
  681. }
  682. /* Per-page copy function for the shmem pwrite fastpath.
  683. * Flushes invalid cachelines before writing to the target if
  684. * needs_clflush_before is set and flushes out any written cachelines after
  685. * writing if needs_clflush is set. */
  686. static int
  687. shmem_pwrite_fast(struct page *page, int shmem_page_offset, int page_length,
  688. char __user *user_data,
  689. bool page_do_bit17_swizzling,
  690. bool needs_clflush_before,
  691. bool needs_clflush_after)
  692. {
  693. char *vaddr;
  694. int ret;
  695. if (unlikely(page_do_bit17_swizzling))
  696. return -EINVAL;
  697. vaddr = kmap_atomic(page);
  698. if (needs_clflush_before)
  699. drm_clflush_virt_range(vaddr + shmem_page_offset,
  700. page_length);
  701. ret = __copy_from_user_inatomic(vaddr + shmem_page_offset,
  702. user_data, page_length);
  703. if (needs_clflush_after)
  704. drm_clflush_virt_range(vaddr + shmem_page_offset,
  705. page_length);
  706. kunmap_atomic(vaddr);
  707. return ret ? -EFAULT : 0;
  708. }
  709. /* Only difference to the fast-path function is that this can handle bit17
  710. * and uses non-atomic copy and kmap functions. */
  711. static int
  712. shmem_pwrite_slow(struct page *page, int shmem_page_offset, int page_length,
  713. char __user *user_data,
  714. bool page_do_bit17_swizzling,
  715. bool needs_clflush_before,
  716. bool needs_clflush_after)
  717. {
  718. char *vaddr;
  719. int ret;
  720. vaddr = kmap(page);
  721. if (unlikely(needs_clflush_before || page_do_bit17_swizzling))
  722. shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
  723. page_length,
  724. page_do_bit17_swizzling);
  725. if (page_do_bit17_swizzling)
  726. ret = __copy_from_user_swizzled(vaddr, shmem_page_offset,
  727. user_data,
  728. page_length);
  729. else
  730. ret = __copy_from_user(vaddr + shmem_page_offset,
  731. user_data,
  732. page_length);
  733. if (needs_clflush_after)
  734. shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
  735. page_length,
  736. page_do_bit17_swizzling);
  737. kunmap(page);
  738. return ret ? -EFAULT : 0;
  739. }
  740. static int
  741. i915_gem_shmem_pwrite(struct drm_device *dev,
  742. struct drm_i915_gem_object *obj,
  743. struct drm_i915_gem_pwrite *args,
  744. struct drm_file *file)
  745. {
  746. ssize_t remain;
  747. loff_t offset;
  748. char __user *user_data;
  749. int shmem_page_offset, page_length, ret = 0;
  750. int obj_do_bit17_swizzling, page_do_bit17_swizzling;
  751. int hit_slowpath = 0;
  752. int needs_clflush_after = 0;
  753. int needs_clflush_before = 0;
  754. struct sg_page_iter sg_iter;
  755. user_data = to_user_ptr(args->data_ptr);
  756. remain = args->size;
  757. obj_do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
  758. if (obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
  759. /* If we're not in the cpu write domain, set ourself into the gtt
  760. * write domain and manually flush cachelines (if required). This
  761. * optimizes for the case when the gpu will use the data
  762. * right away and we therefore have to clflush anyway. */
  763. needs_clflush_after = cpu_write_needs_clflush(obj);
  764. ret = i915_gem_object_wait_rendering(obj, false);
  765. if (ret)
  766. return ret;
  767. }
  768. /* Same trick applies to invalidate partially written cachelines read
  769. * before writing. */
  770. if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0)
  771. needs_clflush_before =
  772. !cpu_cache_is_coherent(dev, obj->cache_level);
  773. ret = i915_gem_object_get_pages(obj);
  774. if (ret)
  775. return ret;
  776. intel_fb_obj_invalidate(obj, ORIGIN_CPU);
  777. i915_gem_object_pin_pages(obj);
  778. offset = args->offset;
  779. obj->dirty = 1;
  780. for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents,
  781. offset >> PAGE_SHIFT) {
  782. struct page *page = sg_page_iter_page(&sg_iter);
  783. int partial_cacheline_write;
  784. if (remain <= 0)
  785. break;
  786. /* Operation in this page
  787. *
  788. * shmem_page_offset = offset within page in shmem file
  789. * page_length = bytes to copy for this page
  790. */
  791. shmem_page_offset = offset_in_page(offset);
  792. page_length = remain;
  793. if ((shmem_page_offset + page_length) > PAGE_SIZE)
  794. page_length = PAGE_SIZE - shmem_page_offset;
  795. /* If we don't overwrite a cacheline completely we need to be
  796. * careful to have up-to-date data by first clflushing. Don't
  797. * overcomplicate things and flush the entire patch. */
  798. partial_cacheline_write = needs_clflush_before &&
  799. ((shmem_page_offset | page_length)
  800. & (boot_cpu_data.x86_clflush_size - 1));
  801. page_do_bit17_swizzling = obj_do_bit17_swizzling &&
  802. (page_to_phys(page) & (1 << 17)) != 0;
  803. ret = shmem_pwrite_fast(page, shmem_page_offset, page_length,
  804. user_data, page_do_bit17_swizzling,
  805. partial_cacheline_write,
  806. needs_clflush_after);
  807. if (ret == 0)
  808. goto next_page;
  809. hit_slowpath = 1;
  810. mutex_unlock(&dev->struct_mutex);
  811. ret = shmem_pwrite_slow(page, shmem_page_offset, page_length,
  812. user_data, page_do_bit17_swizzling,
  813. partial_cacheline_write,
  814. needs_clflush_after);
  815. mutex_lock(&dev->struct_mutex);
  816. if (ret)
  817. goto out;
  818. next_page:
  819. remain -= page_length;
  820. user_data += page_length;
  821. offset += page_length;
  822. }
  823. out:
  824. i915_gem_object_unpin_pages(obj);
  825. if (hit_slowpath) {
  826. /*
  827. * Fixup: Flush cpu caches in case we didn't flush the dirty
  828. * cachelines in-line while writing and the object moved
  829. * out of the cpu write domain while we've dropped the lock.
  830. */
  831. if (!needs_clflush_after &&
  832. obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
  833. if (i915_gem_clflush_object(obj, obj->pin_display))
  834. needs_clflush_after = true;
  835. }
  836. }
  837. if (needs_clflush_after)
  838. i915_gem_chipset_flush(to_i915(dev));
  839. else
  840. obj->cache_dirty = true;
  841. intel_fb_obj_flush(obj, false, ORIGIN_CPU);
  842. return ret;
  843. }
  844. /**
  845. * Writes data to the object referenced by handle.
  846. *
  847. * On error, the contents of the buffer that were to be modified are undefined.
  848. */
  849. int
  850. i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
  851. struct drm_file *file)
  852. {
  853. struct drm_i915_private *dev_priv = dev->dev_private;
  854. struct drm_i915_gem_pwrite *args = data;
  855. struct drm_i915_gem_object *obj;
  856. int ret;
  857. if (args->size == 0)
  858. return 0;
  859. if (!access_ok(VERIFY_READ,
  860. to_user_ptr(args->data_ptr),
  861. args->size))
  862. return -EFAULT;
  863. if (likely(!i915.prefault_disable)) {
  864. ret = fault_in_multipages_readable(to_user_ptr(args->data_ptr),
  865. args->size);
  866. if (ret)
  867. return -EFAULT;
  868. }
  869. intel_runtime_pm_get(dev_priv);
  870. ret = i915_mutex_lock_interruptible(dev);
  871. if (ret)
  872. goto put_rpm;
  873. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  874. if (&obj->base == NULL) {
  875. ret = -ENOENT;
  876. goto unlock;
  877. }
  878. /* Bounds check destination. */
  879. if (args->offset > obj->base.size ||
  880. args->size > obj->base.size - args->offset) {
  881. ret = -EINVAL;
  882. goto out;
  883. }
  884. /* prime objects have no backing filp to GEM pread/pwrite
  885. * pages from.
  886. */
  887. if (!obj->base.filp) {
  888. ret = -EINVAL;
  889. goto out;
  890. }
  891. trace_i915_gem_object_pwrite(obj, args->offset, args->size);
  892. ret = -EFAULT;
  893. /* We can only do the GTT pwrite on untiled buffers, as otherwise
  894. * it would end up going through the fenced access, and we'll get
  895. * different detiling behavior between reading and writing.
  896. * pread/pwrite currently are reading and writing from the CPU
  897. * perspective, requiring manual detiling by the client.
  898. */
  899. if (obj->tiling_mode == I915_TILING_NONE &&
  900. obj->base.write_domain != I915_GEM_DOMAIN_CPU &&
  901. cpu_write_needs_clflush(obj)) {
  902. ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file);
  903. /* Note that the gtt paths might fail with non-page-backed user
  904. * pointers (e.g. gtt mappings when moving data between
  905. * textures). Fallback to the shmem path in that case. */
  906. }
  907. if (ret == -EFAULT || ret == -ENOSPC) {
  908. if (obj->phys_handle)
  909. ret = i915_gem_phys_pwrite(obj, args, file);
  910. else
  911. ret = i915_gem_shmem_pwrite(dev, obj, args, file);
  912. }
  913. out:
  914. drm_gem_object_unreference(&obj->base);
  915. unlock:
  916. mutex_unlock(&dev->struct_mutex);
  917. put_rpm:
  918. intel_runtime_pm_put(dev_priv);
  919. return ret;
  920. }
  921. static int
  922. i915_gem_check_wedge(unsigned reset_counter, bool interruptible)
  923. {
  924. if (__i915_terminally_wedged(reset_counter))
  925. return -EIO;
  926. if (__i915_reset_in_progress(reset_counter)) {
  927. /* Non-interruptible callers can't handle -EAGAIN, hence return
  928. * -EIO unconditionally for these. */
  929. if (!interruptible)
  930. return -EIO;
  931. return -EAGAIN;
  932. }
  933. return 0;
  934. }
  935. static void fake_irq(unsigned long data)
  936. {
  937. wake_up_process((struct task_struct *)data);
  938. }
  939. static bool missed_irq(struct drm_i915_private *dev_priv,
  940. struct intel_engine_cs *engine)
  941. {
  942. return test_bit(engine->id, &dev_priv->gpu_error.missed_irq_rings);
  943. }
  944. static unsigned long local_clock_us(unsigned *cpu)
  945. {
  946. unsigned long t;
  947. /* Cheaply and approximately convert from nanoseconds to microseconds.
  948. * The result and subsequent calculations are also defined in the same
  949. * approximate microseconds units. The principal source of timing
  950. * error here is from the simple truncation.
  951. *
  952. * Note that local_clock() is only defined wrt to the current CPU;
  953. * the comparisons are no longer valid if we switch CPUs. Instead of
  954. * blocking preemption for the entire busywait, we can detect the CPU
  955. * switch and use that as indicator of system load and a reason to
  956. * stop busywaiting, see busywait_stop().
  957. */
  958. *cpu = get_cpu();
  959. t = local_clock() >> 10;
  960. put_cpu();
  961. return t;
  962. }
  963. static bool busywait_stop(unsigned long timeout, unsigned cpu)
  964. {
  965. unsigned this_cpu;
  966. if (time_after(local_clock_us(&this_cpu), timeout))
  967. return true;
  968. return this_cpu != cpu;
  969. }
  970. static int __i915_spin_request(struct drm_i915_gem_request *req, int state)
  971. {
  972. unsigned long timeout;
  973. unsigned cpu;
  974. /* When waiting for high frequency requests, e.g. during synchronous
  975. * rendering split between the CPU and GPU, the finite amount of time
  976. * required to set up the irq and wait upon it limits the response
  977. * rate. By busywaiting on the request completion for a short while we
  978. * can service the high frequency waits as quick as possible. However,
  979. * if it is a slow request, we want to sleep as quickly as possible.
  980. * The tradeoff between waiting and sleeping is roughly the time it
  981. * takes to sleep on a request, on the order of a microsecond.
  982. */
  983. if (req->engine->irq_refcount)
  984. return -EBUSY;
  985. /* Only spin if we know the GPU is processing this request */
  986. if (!i915_gem_request_started(req, true))
  987. return -EAGAIN;
  988. timeout = local_clock_us(&cpu) + 5;
  989. while (!need_resched()) {
  990. if (i915_gem_request_completed(req, true))
  991. return 0;
  992. if (signal_pending_state(state, current))
  993. break;
  994. if (busywait_stop(timeout, cpu))
  995. break;
  996. cpu_relax_lowlatency();
  997. }
  998. if (i915_gem_request_completed(req, false))
  999. return 0;
  1000. return -EAGAIN;
  1001. }
  1002. /**
  1003. * __i915_wait_request - wait until execution of request has finished
  1004. * @req: duh!
  1005. * @interruptible: do an interruptible wait (normally yes)
  1006. * @timeout: in - how long to wait (NULL forever); out - how much time remaining
  1007. *
  1008. * Note: It is of utmost importance that the passed in seqno and reset_counter
  1009. * values have been read by the caller in an smp safe manner. Where read-side
  1010. * locks are involved, it is sufficient to read the reset_counter before
  1011. * unlocking the lock that protects the seqno. For lockless tricks, the
  1012. * reset_counter _must_ be read before, and an appropriate smp_rmb must be
  1013. * inserted.
  1014. *
  1015. * Returns 0 if the request was found within the alloted time. Else returns the
  1016. * errno with remaining time filled in timeout argument.
  1017. */
  1018. int __i915_wait_request(struct drm_i915_gem_request *req,
  1019. bool interruptible,
  1020. s64 *timeout,
  1021. struct intel_rps_client *rps)
  1022. {
  1023. struct intel_engine_cs *engine = i915_gem_request_get_engine(req);
  1024. struct drm_i915_private *dev_priv = req->i915;
  1025. const bool irq_test_in_progress =
  1026. ACCESS_ONCE(dev_priv->gpu_error.test_irq_rings) & intel_engine_flag(engine);
  1027. int state = interruptible ? TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE;
  1028. DEFINE_WAIT(wait);
  1029. unsigned long timeout_expire;
  1030. s64 before = 0; /* Only to silence a compiler warning. */
  1031. int ret;
  1032. WARN(!intel_irqs_enabled(dev_priv), "IRQs disabled");
  1033. if (list_empty(&req->list))
  1034. return 0;
  1035. if (i915_gem_request_completed(req, true))
  1036. return 0;
  1037. timeout_expire = 0;
  1038. if (timeout) {
  1039. if (WARN_ON(*timeout < 0))
  1040. return -EINVAL;
  1041. if (*timeout == 0)
  1042. return -ETIME;
  1043. timeout_expire = jiffies + nsecs_to_jiffies_timeout(*timeout);
  1044. /*
  1045. * Record current time in case interrupted by signal, or wedged.
  1046. */
  1047. before = ktime_get_raw_ns();
  1048. }
  1049. if (INTEL_INFO(dev_priv)->gen >= 6)
  1050. gen6_rps_boost(dev_priv, rps, req->emitted_jiffies);
  1051. trace_i915_gem_request_wait_begin(req);
  1052. /* Optimistic spin for the next jiffie before touching IRQs */
  1053. ret = __i915_spin_request(req, state);
  1054. if (ret == 0)
  1055. goto out;
  1056. if (!irq_test_in_progress && WARN_ON(!engine->irq_get(engine))) {
  1057. ret = -ENODEV;
  1058. goto out;
  1059. }
  1060. for (;;) {
  1061. struct timer_list timer;
  1062. prepare_to_wait(&engine->irq_queue, &wait, state);
  1063. /* We need to check whether any gpu reset happened in between
  1064. * the request being submitted and now. If a reset has occurred,
  1065. * the request is effectively complete (we either are in the
  1066. * process of or have discarded the rendering and completely
  1067. * reset the GPU. The results of the request are lost and we
  1068. * are free to continue on with the original operation.
  1069. */
  1070. if (req->reset_counter != i915_reset_counter(&dev_priv->gpu_error)) {
  1071. ret = 0;
  1072. break;
  1073. }
  1074. if (i915_gem_request_completed(req, false)) {
  1075. ret = 0;
  1076. break;
  1077. }
  1078. if (signal_pending_state(state, current)) {
  1079. ret = -ERESTARTSYS;
  1080. break;
  1081. }
  1082. if (timeout && time_after_eq(jiffies, timeout_expire)) {
  1083. ret = -ETIME;
  1084. break;
  1085. }
  1086. timer.function = NULL;
  1087. if (timeout || missed_irq(dev_priv, engine)) {
  1088. unsigned long expire;
  1089. setup_timer_on_stack(&timer, fake_irq, (unsigned long)current);
  1090. expire = missed_irq(dev_priv, engine) ? jiffies + 1 : timeout_expire;
  1091. mod_timer(&timer, expire);
  1092. }
  1093. io_schedule();
  1094. if (timer.function) {
  1095. del_singleshot_timer_sync(&timer);
  1096. destroy_timer_on_stack(&timer);
  1097. }
  1098. }
  1099. if (!irq_test_in_progress)
  1100. engine->irq_put(engine);
  1101. finish_wait(&engine->irq_queue, &wait);
  1102. out:
  1103. trace_i915_gem_request_wait_end(req);
  1104. if (timeout) {
  1105. s64 tres = *timeout - (ktime_get_raw_ns() - before);
  1106. *timeout = tres < 0 ? 0 : tres;
  1107. /*
  1108. * Apparently ktime isn't accurate enough and occasionally has a
  1109. * bit of mismatch in the jiffies<->nsecs<->ktime loop. So patch
  1110. * things up to make the test happy. We allow up to 1 jiffy.
  1111. *
  1112. * This is a regrssion from the timespec->ktime conversion.
  1113. */
  1114. if (ret == -ETIME && *timeout < jiffies_to_usecs(1)*1000)
  1115. *timeout = 0;
  1116. }
  1117. return ret;
  1118. }
  1119. int i915_gem_request_add_to_client(struct drm_i915_gem_request *req,
  1120. struct drm_file *file)
  1121. {
  1122. struct drm_i915_file_private *file_priv;
  1123. WARN_ON(!req || !file || req->file_priv);
  1124. if (!req || !file)
  1125. return -EINVAL;
  1126. if (req->file_priv)
  1127. return -EINVAL;
  1128. file_priv = file->driver_priv;
  1129. spin_lock(&file_priv->mm.lock);
  1130. req->file_priv = file_priv;
  1131. list_add_tail(&req->client_list, &file_priv->mm.request_list);
  1132. spin_unlock(&file_priv->mm.lock);
  1133. req->pid = get_pid(task_pid(current));
  1134. return 0;
  1135. }
  1136. static inline void
  1137. i915_gem_request_remove_from_client(struct drm_i915_gem_request *request)
  1138. {
  1139. struct drm_i915_file_private *file_priv = request->file_priv;
  1140. if (!file_priv)
  1141. return;
  1142. spin_lock(&file_priv->mm.lock);
  1143. list_del(&request->client_list);
  1144. request->file_priv = NULL;
  1145. spin_unlock(&file_priv->mm.lock);
  1146. put_pid(request->pid);
  1147. request->pid = NULL;
  1148. }
  1149. static void i915_gem_request_retire(struct drm_i915_gem_request *request)
  1150. {
  1151. trace_i915_gem_request_retire(request);
  1152. /* We know the GPU must have read the request to have
  1153. * sent us the seqno + interrupt, so use the position
  1154. * of tail of the request to update the last known position
  1155. * of the GPU head.
  1156. *
  1157. * Note this requires that we are always called in request
  1158. * completion order.
  1159. */
  1160. request->ringbuf->last_retired_head = request->postfix;
  1161. list_del_init(&request->list);
  1162. i915_gem_request_remove_from_client(request);
  1163. if (request->previous_context) {
  1164. if (i915.enable_execlists)
  1165. intel_lr_context_unpin(request->previous_context,
  1166. request->engine);
  1167. }
  1168. i915_gem_context_unreference(request->ctx);
  1169. i915_gem_request_unreference(request);
  1170. }
  1171. static void
  1172. __i915_gem_request_retire__upto(struct drm_i915_gem_request *req)
  1173. {
  1174. struct intel_engine_cs *engine = req->engine;
  1175. struct drm_i915_gem_request *tmp;
  1176. lockdep_assert_held(&engine->i915->dev->struct_mutex);
  1177. if (list_empty(&req->list))
  1178. return;
  1179. do {
  1180. tmp = list_first_entry(&engine->request_list,
  1181. typeof(*tmp), list);
  1182. i915_gem_request_retire(tmp);
  1183. } while (tmp != req);
  1184. WARN_ON(i915_verify_lists(engine->dev));
  1185. }
  1186. /**
  1187. * Waits for a request to be signaled, and cleans up the
  1188. * request and object lists appropriately for that event.
  1189. */
  1190. int
  1191. i915_wait_request(struct drm_i915_gem_request *req)
  1192. {
  1193. struct drm_i915_private *dev_priv = req->i915;
  1194. bool interruptible;
  1195. int ret;
  1196. interruptible = dev_priv->mm.interruptible;
  1197. BUG_ON(!mutex_is_locked(&dev_priv->dev->struct_mutex));
  1198. ret = __i915_wait_request(req, interruptible, NULL, NULL);
  1199. if (ret)
  1200. return ret;
  1201. /* If the GPU hung, we want to keep the requests to find the guilty. */
  1202. if (req->reset_counter == i915_reset_counter(&dev_priv->gpu_error))
  1203. __i915_gem_request_retire__upto(req);
  1204. return 0;
  1205. }
  1206. /**
  1207. * Ensures that all rendering to the object has completed and the object is
  1208. * safe to unbind from the GTT or access from the CPU.
  1209. */
  1210. int
  1211. i915_gem_object_wait_rendering(struct drm_i915_gem_object *obj,
  1212. bool readonly)
  1213. {
  1214. int ret, i;
  1215. if (!obj->active)
  1216. return 0;
  1217. if (readonly) {
  1218. if (obj->last_write_req != NULL) {
  1219. ret = i915_wait_request(obj->last_write_req);
  1220. if (ret)
  1221. return ret;
  1222. i = obj->last_write_req->engine->id;
  1223. if (obj->last_read_req[i] == obj->last_write_req)
  1224. i915_gem_object_retire__read(obj, i);
  1225. else
  1226. i915_gem_object_retire__write(obj);
  1227. }
  1228. } else {
  1229. for (i = 0; i < I915_NUM_ENGINES; i++) {
  1230. if (obj->last_read_req[i] == NULL)
  1231. continue;
  1232. ret = i915_wait_request(obj->last_read_req[i]);
  1233. if (ret)
  1234. return ret;
  1235. i915_gem_object_retire__read(obj, i);
  1236. }
  1237. GEM_BUG_ON(obj->active);
  1238. }
  1239. return 0;
  1240. }
  1241. static void
  1242. i915_gem_object_retire_request(struct drm_i915_gem_object *obj,
  1243. struct drm_i915_gem_request *req)
  1244. {
  1245. int ring = req->engine->id;
  1246. if (obj->last_read_req[ring] == req)
  1247. i915_gem_object_retire__read(obj, ring);
  1248. else if (obj->last_write_req == req)
  1249. i915_gem_object_retire__write(obj);
  1250. if (req->reset_counter == i915_reset_counter(&req->i915->gpu_error))
  1251. __i915_gem_request_retire__upto(req);
  1252. }
  1253. /* A nonblocking variant of the above wait. This is a highly dangerous routine
  1254. * as the object state may change during this call.
  1255. */
  1256. static __must_check int
  1257. i915_gem_object_wait_rendering__nonblocking(struct drm_i915_gem_object *obj,
  1258. struct intel_rps_client *rps,
  1259. bool readonly)
  1260. {
  1261. struct drm_device *dev = obj->base.dev;
  1262. struct drm_i915_private *dev_priv = dev->dev_private;
  1263. struct drm_i915_gem_request *requests[I915_NUM_ENGINES];
  1264. int ret, i, n = 0;
  1265. BUG_ON(!mutex_is_locked(&dev->struct_mutex));
  1266. BUG_ON(!dev_priv->mm.interruptible);
  1267. if (!obj->active)
  1268. return 0;
  1269. if (readonly) {
  1270. struct drm_i915_gem_request *req;
  1271. req = obj->last_write_req;
  1272. if (req == NULL)
  1273. return 0;
  1274. requests[n++] = i915_gem_request_reference(req);
  1275. } else {
  1276. for (i = 0; i < I915_NUM_ENGINES; i++) {
  1277. struct drm_i915_gem_request *req;
  1278. req = obj->last_read_req[i];
  1279. if (req == NULL)
  1280. continue;
  1281. requests[n++] = i915_gem_request_reference(req);
  1282. }
  1283. }
  1284. mutex_unlock(&dev->struct_mutex);
  1285. ret = 0;
  1286. for (i = 0; ret == 0 && i < n; i++)
  1287. ret = __i915_wait_request(requests[i], true, NULL, rps);
  1288. mutex_lock(&dev->struct_mutex);
  1289. for (i = 0; i < n; i++) {
  1290. if (ret == 0)
  1291. i915_gem_object_retire_request(obj, requests[i]);
  1292. i915_gem_request_unreference(requests[i]);
  1293. }
  1294. return ret;
  1295. }
  1296. static struct intel_rps_client *to_rps_client(struct drm_file *file)
  1297. {
  1298. struct drm_i915_file_private *fpriv = file->driver_priv;
  1299. return &fpriv->rps;
  1300. }
  1301. /**
  1302. * Called when user space prepares to use an object with the CPU, either
  1303. * through the mmap ioctl's mapping or a GTT mapping.
  1304. */
  1305. int
  1306. i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
  1307. struct drm_file *file)
  1308. {
  1309. struct drm_i915_gem_set_domain *args = data;
  1310. struct drm_i915_gem_object *obj;
  1311. uint32_t read_domains = args->read_domains;
  1312. uint32_t write_domain = args->write_domain;
  1313. int ret;
  1314. /* Only handle setting domains to types used by the CPU. */
  1315. if (write_domain & I915_GEM_GPU_DOMAINS)
  1316. return -EINVAL;
  1317. if (read_domains & I915_GEM_GPU_DOMAINS)
  1318. return -EINVAL;
  1319. /* Having something in the write domain implies it's in the read
  1320. * domain, and only that read domain. Enforce that in the request.
  1321. */
  1322. if (write_domain != 0 && read_domains != write_domain)
  1323. return -EINVAL;
  1324. ret = i915_mutex_lock_interruptible(dev);
  1325. if (ret)
  1326. return ret;
  1327. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  1328. if (&obj->base == NULL) {
  1329. ret = -ENOENT;
  1330. goto unlock;
  1331. }
  1332. /* Try to flush the object off the GPU without holding the lock.
  1333. * We will repeat the flush holding the lock in the normal manner
  1334. * to catch cases where we are gazumped.
  1335. */
  1336. ret = i915_gem_object_wait_rendering__nonblocking(obj,
  1337. to_rps_client(file),
  1338. !write_domain);
  1339. if (ret)
  1340. goto unref;
  1341. if (read_domains & I915_GEM_DOMAIN_GTT)
  1342. ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
  1343. else
  1344. ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
  1345. if (write_domain != 0)
  1346. intel_fb_obj_invalidate(obj,
  1347. write_domain == I915_GEM_DOMAIN_GTT ?
  1348. ORIGIN_GTT : ORIGIN_CPU);
  1349. unref:
  1350. drm_gem_object_unreference(&obj->base);
  1351. unlock:
  1352. mutex_unlock(&dev->struct_mutex);
  1353. return ret;
  1354. }
  1355. /**
  1356. * Called when user space has done writes to this buffer
  1357. */
  1358. int
  1359. i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
  1360. struct drm_file *file)
  1361. {
  1362. struct drm_i915_gem_sw_finish *args = data;
  1363. struct drm_i915_gem_object *obj;
  1364. int ret = 0;
  1365. ret = i915_mutex_lock_interruptible(dev);
  1366. if (ret)
  1367. return ret;
  1368. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  1369. if (&obj->base == NULL) {
  1370. ret = -ENOENT;
  1371. goto unlock;
  1372. }
  1373. /* Pinned buffers may be scanout, so flush the cache */
  1374. if (obj->pin_display)
  1375. i915_gem_object_flush_cpu_write_domain(obj);
  1376. drm_gem_object_unreference(&obj->base);
  1377. unlock:
  1378. mutex_unlock(&dev->struct_mutex);
  1379. return ret;
  1380. }
  1381. /**
  1382. * Maps the contents of an object, returning the address it is mapped
  1383. * into.
  1384. *
  1385. * While the mapping holds a reference on the contents of the object, it doesn't
  1386. * imply a ref on the object itself.
  1387. *
  1388. * IMPORTANT:
  1389. *
  1390. * DRM driver writers who look a this function as an example for how to do GEM
  1391. * mmap support, please don't implement mmap support like here. The modern way
  1392. * to implement DRM mmap support is with an mmap offset ioctl (like
  1393. * i915_gem_mmap_gtt) and then using the mmap syscall on the DRM fd directly.
  1394. * That way debug tooling like valgrind will understand what's going on, hiding
  1395. * the mmap call in a driver private ioctl will break that. The i915 driver only
  1396. * does cpu mmaps this way because we didn't know better.
  1397. */
  1398. int
  1399. i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
  1400. struct drm_file *file)
  1401. {
  1402. struct drm_i915_gem_mmap *args = data;
  1403. struct drm_gem_object *obj;
  1404. unsigned long addr;
  1405. if (args->flags & ~(I915_MMAP_WC))
  1406. return -EINVAL;
  1407. if (args->flags & I915_MMAP_WC && !cpu_has_pat)
  1408. return -ENODEV;
  1409. obj = drm_gem_object_lookup(dev, file, args->handle);
  1410. if (obj == NULL)
  1411. return -ENOENT;
  1412. /* prime objects have no backing filp to GEM mmap
  1413. * pages from.
  1414. */
  1415. if (!obj->filp) {
  1416. drm_gem_object_unreference_unlocked(obj);
  1417. return -EINVAL;
  1418. }
  1419. addr = vm_mmap(obj->filp, 0, args->size,
  1420. PROT_READ | PROT_WRITE, MAP_SHARED,
  1421. args->offset);
  1422. if (args->flags & I915_MMAP_WC) {
  1423. struct mm_struct *mm = current->mm;
  1424. struct vm_area_struct *vma;
  1425. down_write(&mm->mmap_sem);
  1426. vma = find_vma(mm, addr);
  1427. if (vma)
  1428. vma->vm_page_prot =
  1429. pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
  1430. else
  1431. addr = -ENOMEM;
  1432. up_write(&mm->mmap_sem);
  1433. }
  1434. drm_gem_object_unreference_unlocked(obj);
  1435. if (IS_ERR((void *)addr))
  1436. return addr;
  1437. args->addr_ptr = (uint64_t) addr;
  1438. return 0;
  1439. }
  1440. /**
  1441. * i915_gem_fault - fault a page into the GTT
  1442. * @vma: VMA in question
  1443. * @vmf: fault info
  1444. *
  1445. * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
  1446. * from userspace. The fault handler takes care of binding the object to
  1447. * the GTT (if needed), allocating and programming a fence register (again,
  1448. * only if needed based on whether the old reg is still valid or the object
  1449. * is tiled) and inserting a new PTE into the faulting process.
  1450. *
  1451. * Note that the faulting process may involve evicting existing objects
  1452. * from the GTT and/or fence registers to make room. So performance may
  1453. * suffer if the GTT working set is large or there are few fence registers
  1454. * left.
  1455. */
  1456. int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  1457. {
  1458. struct drm_i915_gem_object *obj = to_intel_bo(vma->vm_private_data);
  1459. struct drm_device *dev = obj->base.dev;
  1460. struct drm_i915_private *dev_priv = to_i915(dev);
  1461. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  1462. struct i915_ggtt_view view = i915_ggtt_view_normal;
  1463. pgoff_t page_offset;
  1464. unsigned long pfn;
  1465. int ret = 0;
  1466. bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
  1467. intel_runtime_pm_get(dev_priv);
  1468. /* We don't use vmf->pgoff since that has the fake offset */
  1469. page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
  1470. PAGE_SHIFT;
  1471. ret = i915_mutex_lock_interruptible(dev);
  1472. if (ret)
  1473. goto out;
  1474. trace_i915_gem_object_fault(obj, page_offset, true, write);
  1475. /* Try to flush the object off the GPU first without holding the lock.
  1476. * Upon reacquiring the lock, we will perform our sanity checks and then
  1477. * repeat the flush holding the lock in the normal manner to catch cases
  1478. * where we are gazumped.
  1479. */
  1480. ret = i915_gem_object_wait_rendering__nonblocking(obj, NULL, !write);
  1481. if (ret)
  1482. goto unlock;
  1483. /* Access to snoopable pages through the GTT is incoherent. */
  1484. if (obj->cache_level != I915_CACHE_NONE && !HAS_LLC(dev)) {
  1485. ret = -EFAULT;
  1486. goto unlock;
  1487. }
  1488. /* Use a partial view if the object is bigger than the aperture. */
  1489. if (obj->base.size >= ggtt->mappable_end &&
  1490. obj->tiling_mode == I915_TILING_NONE) {
  1491. static const unsigned int chunk_size = 256; // 1 MiB
  1492. memset(&view, 0, sizeof(view));
  1493. view.type = I915_GGTT_VIEW_PARTIAL;
  1494. view.params.partial.offset = rounddown(page_offset, chunk_size);
  1495. view.params.partial.size =
  1496. min_t(unsigned int,
  1497. chunk_size,
  1498. (vma->vm_end - vma->vm_start)/PAGE_SIZE -
  1499. view.params.partial.offset);
  1500. }
  1501. /* Now pin it into the GTT if needed */
  1502. ret = i915_gem_object_ggtt_pin(obj, &view, 0, PIN_MAPPABLE);
  1503. if (ret)
  1504. goto unlock;
  1505. ret = i915_gem_object_set_to_gtt_domain(obj, write);
  1506. if (ret)
  1507. goto unpin;
  1508. ret = i915_gem_object_get_fence(obj);
  1509. if (ret)
  1510. goto unpin;
  1511. /* Finally, remap it using the new GTT offset */
  1512. pfn = ggtt->mappable_base +
  1513. i915_gem_obj_ggtt_offset_view(obj, &view);
  1514. pfn >>= PAGE_SHIFT;
  1515. if (unlikely(view.type == I915_GGTT_VIEW_PARTIAL)) {
  1516. /* Overriding existing pages in partial view does not cause
  1517. * us any trouble as TLBs are still valid because the fault
  1518. * is due to userspace losing part of the mapping or never
  1519. * having accessed it before (at this partials' range).
  1520. */
  1521. unsigned long base = vma->vm_start +
  1522. (view.params.partial.offset << PAGE_SHIFT);
  1523. unsigned int i;
  1524. for (i = 0; i < view.params.partial.size; i++) {
  1525. ret = vm_insert_pfn(vma, base + i * PAGE_SIZE, pfn + i);
  1526. if (ret)
  1527. break;
  1528. }
  1529. obj->fault_mappable = true;
  1530. } else {
  1531. if (!obj->fault_mappable) {
  1532. unsigned long size = min_t(unsigned long,
  1533. vma->vm_end - vma->vm_start,
  1534. obj->base.size);
  1535. int i;
  1536. for (i = 0; i < size >> PAGE_SHIFT; i++) {
  1537. ret = vm_insert_pfn(vma,
  1538. (unsigned long)vma->vm_start + i * PAGE_SIZE,
  1539. pfn + i);
  1540. if (ret)
  1541. break;
  1542. }
  1543. obj->fault_mappable = true;
  1544. } else
  1545. ret = vm_insert_pfn(vma,
  1546. (unsigned long)vmf->virtual_address,
  1547. pfn + page_offset);
  1548. }
  1549. unpin:
  1550. i915_gem_object_ggtt_unpin_view(obj, &view);
  1551. unlock:
  1552. mutex_unlock(&dev->struct_mutex);
  1553. out:
  1554. switch (ret) {
  1555. case -EIO:
  1556. /*
  1557. * We eat errors when the gpu is terminally wedged to avoid
  1558. * userspace unduly crashing (gl has no provisions for mmaps to
  1559. * fail). But any other -EIO isn't ours (e.g. swap in failure)
  1560. * and so needs to be reported.
  1561. */
  1562. if (!i915_terminally_wedged(&dev_priv->gpu_error)) {
  1563. ret = VM_FAULT_SIGBUS;
  1564. break;
  1565. }
  1566. case -EAGAIN:
  1567. /*
  1568. * EAGAIN means the gpu is hung and we'll wait for the error
  1569. * handler to reset everything when re-faulting in
  1570. * i915_mutex_lock_interruptible.
  1571. */
  1572. case 0:
  1573. case -ERESTARTSYS:
  1574. case -EINTR:
  1575. case -EBUSY:
  1576. /*
  1577. * EBUSY is ok: this just means that another thread
  1578. * already did the job.
  1579. */
  1580. ret = VM_FAULT_NOPAGE;
  1581. break;
  1582. case -ENOMEM:
  1583. ret = VM_FAULT_OOM;
  1584. break;
  1585. case -ENOSPC:
  1586. case -EFAULT:
  1587. ret = VM_FAULT_SIGBUS;
  1588. break;
  1589. default:
  1590. WARN_ONCE(ret, "unhandled error in i915_gem_fault: %i\n", ret);
  1591. ret = VM_FAULT_SIGBUS;
  1592. break;
  1593. }
  1594. intel_runtime_pm_put(dev_priv);
  1595. return ret;
  1596. }
  1597. /**
  1598. * i915_gem_release_mmap - remove physical page mappings
  1599. * @obj: obj in question
  1600. *
  1601. * Preserve the reservation of the mmapping with the DRM core code, but
  1602. * relinquish ownership of the pages back to the system.
  1603. *
  1604. * It is vital that we remove the page mapping if we have mapped a tiled
  1605. * object through the GTT and then lose the fence register due to
  1606. * resource pressure. Similarly if the object has been moved out of the
  1607. * aperture, than pages mapped into userspace must be revoked. Removing the
  1608. * mapping will then trigger a page fault on the next user access, allowing
  1609. * fixup by i915_gem_fault().
  1610. */
  1611. void
  1612. i915_gem_release_mmap(struct drm_i915_gem_object *obj)
  1613. {
  1614. /* Serialisation between user GTT access and our code depends upon
  1615. * revoking the CPU's PTE whilst the mutex is held. The next user
  1616. * pagefault then has to wait until we release the mutex.
  1617. */
  1618. lockdep_assert_held(&obj->base.dev->struct_mutex);
  1619. if (!obj->fault_mappable)
  1620. return;
  1621. drm_vma_node_unmap(&obj->base.vma_node,
  1622. obj->base.dev->anon_inode->i_mapping);
  1623. /* Ensure that the CPU's PTE are revoked and there are not outstanding
  1624. * memory transactions from userspace before we return. The TLB
  1625. * flushing implied above by changing the PTE above *should* be
  1626. * sufficient, an extra barrier here just provides us with a bit
  1627. * of paranoid documentation about our requirement to serialise
  1628. * memory writes before touching registers / GSM.
  1629. */
  1630. wmb();
  1631. obj->fault_mappable = false;
  1632. }
  1633. void
  1634. i915_gem_release_all_mmaps(struct drm_i915_private *dev_priv)
  1635. {
  1636. struct drm_i915_gem_object *obj;
  1637. list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list)
  1638. i915_gem_release_mmap(obj);
  1639. }
  1640. uint32_t
  1641. i915_gem_get_gtt_size(struct drm_device *dev, uint32_t size, int tiling_mode)
  1642. {
  1643. uint32_t gtt_size;
  1644. if (INTEL_INFO(dev)->gen >= 4 ||
  1645. tiling_mode == I915_TILING_NONE)
  1646. return size;
  1647. /* Previous chips need a power-of-two fence region when tiling */
  1648. if (IS_GEN3(dev))
  1649. gtt_size = 1024*1024;
  1650. else
  1651. gtt_size = 512*1024;
  1652. while (gtt_size < size)
  1653. gtt_size <<= 1;
  1654. return gtt_size;
  1655. }
  1656. /**
  1657. * i915_gem_get_gtt_alignment - return required GTT alignment for an object
  1658. * @obj: object to check
  1659. *
  1660. * Return the required GTT alignment for an object, taking into account
  1661. * potential fence register mapping.
  1662. */
  1663. uint32_t
  1664. i915_gem_get_gtt_alignment(struct drm_device *dev, uint32_t size,
  1665. int tiling_mode, bool fenced)
  1666. {
  1667. /*
  1668. * Minimum alignment is 4k (GTT page size), but might be greater
  1669. * if a fence register is needed for the object.
  1670. */
  1671. if (INTEL_INFO(dev)->gen >= 4 || (!fenced && IS_G33(dev)) ||
  1672. tiling_mode == I915_TILING_NONE)
  1673. return 4096;
  1674. /*
  1675. * Previous chips need to be aligned to the size of the smallest
  1676. * fence register that can contain the object.
  1677. */
  1678. return i915_gem_get_gtt_size(dev, size, tiling_mode);
  1679. }
  1680. static int i915_gem_object_create_mmap_offset(struct drm_i915_gem_object *obj)
  1681. {
  1682. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  1683. int ret;
  1684. dev_priv->mm.shrinker_no_lock_stealing = true;
  1685. ret = drm_gem_create_mmap_offset(&obj->base);
  1686. if (ret != -ENOSPC)
  1687. goto out;
  1688. /* Badly fragmented mmap space? The only way we can recover
  1689. * space is by destroying unwanted objects. We can't randomly release
  1690. * mmap_offsets as userspace expects them to be persistent for the
  1691. * lifetime of the objects. The closest we can is to release the
  1692. * offsets on purgeable objects by truncating it and marking it purged,
  1693. * which prevents userspace from ever using that object again.
  1694. */
  1695. i915_gem_shrink(dev_priv,
  1696. obj->base.size >> PAGE_SHIFT,
  1697. I915_SHRINK_BOUND |
  1698. I915_SHRINK_UNBOUND |
  1699. I915_SHRINK_PURGEABLE);
  1700. ret = drm_gem_create_mmap_offset(&obj->base);
  1701. if (ret != -ENOSPC)
  1702. goto out;
  1703. i915_gem_shrink_all(dev_priv);
  1704. ret = drm_gem_create_mmap_offset(&obj->base);
  1705. out:
  1706. dev_priv->mm.shrinker_no_lock_stealing = false;
  1707. return ret;
  1708. }
  1709. static void i915_gem_object_free_mmap_offset(struct drm_i915_gem_object *obj)
  1710. {
  1711. drm_gem_free_mmap_offset(&obj->base);
  1712. }
  1713. int
  1714. i915_gem_mmap_gtt(struct drm_file *file,
  1715. struct drm_device *dev,
  1716. uint32_t handle,
  1717. uint64_t *offset)
  1718. {
  1719. struct drm_i915_gem_object *obj;
  1720. int ret;
  1721. ret = i915_mutex_lock_interruptible(dev);
  1722. if (ret)
  1723. return ret;
  1724. obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
  1725. if (&obj->base == NULL) {
  1726. ret = -ENOENT;
  1727. goto unlock;
  1728. }
  1729. if (obj->madv != I915_MADV_WILLNEED) {
  1730. DRM_DEBUG("Attempting to mmap a purgeable buffer\n");
  1731. ret = -EFAULT;
  1732. goto out;
  1733. }
  1734. ret = i915_gem_object_create_mmap_offset(obj);
  1735. if (ret)
  1736. goto out;
  1737. *offset = drm_vma_node_offset_addr(&obj->base.vma_node);
  1738. out:
  1739. drm_gem_object_unreference(&obj->base);
  1740. unlock:
  1741. mutex_unlock(&dev->struct_mutex);
  1742. return ret;
  1743. }
  1744. /**
  1745. * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
  1746. * @dev: DRM device
  1747. * @data: GTT mapping ioctl data
  1748. * @file: GEM object info
  1749. *
  1750. * Simply returns the fake offset to userspace so it can mmap it.
  1751. * The mmap call will end up in drm_gem_mmap(), which will set things
  1752. * up so we can get faults in the handler above.
  1753. *
  1754. * The fault handler will take care of binding the object into the GTT
  1755. * (since it may have been evicted to make room for something), allocating
  1756. * a fence register, and mapping the appropriate aperture address into
  1757. * userspace.
  1758. */
  1759. int
  1760. i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
  1761. struct drm_file *file)
  1762. {
  1763. struct drm_i915_gem_mmap_gtt *args = data;
  1764. return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset);
  1765. }
  1766. /* Immediately discard the backing storage */
  1767. static void
  1768. i915_gem_object_truncate(struct drm_i915_gem_object *obj)
  1769. {
  1770. i915_gem_object_free_mmap_offset(obj);
  1771. if (obj->base.filp == NULL)
  1772. return;
  1773. /* Our goal here is to return as much of the memory as
  1774. * is possible back to the system as we are called from OOM.
  1775. * To do this we must instruct the shmfs to drop all of its
  1776. * backing pages, *now*.
  1777. */
  1778. shmem_truncate_range(file_inode(obj->base.filp), 0, (loff_t)-1);
  1779. obj->madv = __I915_MADV_PURGED;
  1780. }
  1781. /* Try to discard unwanted pages */
  1782. static void
  1783. i915_gem_object_invalidate(struct drm_i915_gem_object *obj)
  1784. {
  1785. struct address_space *mapping;
  1786. switch (obj->madv) {
  1787. case I915_MADV_DONTNEED:
  1788. i915_gem_object_truncate(obj);
  1789. case __I915_MADV_PURGED:
  1790. return;
  1791. }
  1792. if (obj->base.filp == NULL)
  1793. return;
  1794. mapping = file_inode(obj->base.filp)->i_mapping,
  1795. invalidate_mapping_pages(mapping, 0, (loff_t)-1);
  1796. }
  1797. static void
  1798. i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj)
  1799. {
  1800. struct sg_page_iter sg_iter;
  1801. int ret;
  1802. BUG_ON(obj->madv == __I915_MADV_PURGED);
  1803. ret = i915_gem_object_set_to_cpu_domain(obj, true);
  1804. if (WARN_ON(ret)) {
  1805. /* In the event of a disaster, abandon all caches and
  1806. * hope for the best.
  1807. */
  1808. i915_gem_clflush_object(obj, true);
  1809. obj->base.read_domains = obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  1810. }
  1811. i915_gem_gtt_finish_object(obj);
  1812. if (i915_gem_object_needs_bit17_swizzle(obj))
  1813. i915_gem_object_save_bit_17_swizzle(obj);
  1814. if (obj->madv == I915_MADV_DONTNEED)
  1815. obj->dirty = 0;
  1816. for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents, 0) {
  1817. struct page *page = sg_page_iter_page(&sg_iter);
  1818. if (obj->dirty)
  1819. set_page_dirty(page);
  1820. if (obj->madv == I915_MADV_WILLNEED)
  1821. mark_page_accessed(page);
  1822. put_page(page);
  1823. }
  1824. obj->dirty = 0;
  1825. sg_free_table(obj->pages);
  1826. kfree(obj->pages);
  1827. }
  1828. int
  1829. i915_gem_object_put_pages(struct drm_i915_gem_object *obj)
  1830. {
  1831. const struct drm_i915_gem_object_ops *ops = obj->ops;
  1832. if (obj->pages == NULL)
  1833. return 0;
  1834. if (obj->pages_pin_count)
  1835. return -EBUSY;
  1836. BUG_ON(i915_gem_obj_bound_any(obj));
  1837. /* ->put_pages might need to allocate memory for the bit17 swizzle
  1838. * array, hence protect them from being reaped by removing them from gtt
  1839. * lists early. */
  1840. list_del(&obj->global_list);
  1841. if (obj->mapping) {
  1842. if (is_vmalloc_addr(obj->mapping))
  1843. vunmap(obj->mapping);
  1844. else
  1845. kunmap(kmap_to_page(obj->mapping));
  1846. obj->mapping = NULL;
  1847. }
  1848. ops->put_pages(obj);
  1849. obj->pages = NULL;
  1850. i915_gem_object_invalidate(obj);
  1851. return 0;
  1852. }
  1853. static int
  1854. i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj)
  1855. {
  1856. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  1857. int page_count, i;
  1858. struct address_space *mapping;
  1859. struct sg_table *st;
  1860. struct scatterlist *sg;
  1861. struct sg_page_iter sg_iter;
  1862. struct page *page;
  1863. unsigned long last_pfn = 0; /* suppress gcc warning */
  1864. int ret;
  1865. gfp_t gfp;
  1866. /* Assert that the object is not currently in any GPU domain. As it
  1867. * wasn't in the GTT, there shouldn't be any way it could have been in
  1868. * a GPU cache
  1869. */
  1870. BUG_ON(obj->base.read_domains & I915_GEM_GPU_DOMAINS);
  1871. BUG_ON(obj->base.write_domain & I915_GEM_GPU_DOMAINS);
  1872. st = kmalloc(sizeof(*st), GFP_KERNEL);
  1873. if (st == NULL)
  1874. return -ENOMEM;
  1875. page_count = obj->base.size / PAGE_SIZE;
  1876. if (sg_alloc_table(st, page_count, GFP_KERNEL)) {
  1877. kfree(st);
  1878. return -ENOMEM;
  1879. }
  1880. /* Get the list of pages out of our struct file. They'll be pinned
  1881. * at this point until we release them.
  1882. *
  1883. * Fail silently without starting the shrinker
  1884. */
  1885. mapping = file_inode(obj->base.filp)->i_mapping;
  1886. gfp = mapping_gfp_constraint(mapping, ~(__GFP_IO | __GFP_RECLAIM));
  1887. gfp |= __GFP_NORETRY | __GFP_NOWARN;
  1888. sg = st->sgl;
  1889. st->nents = 0;
  1890. for (i = 0; i < page_count; i++) {
  1891. page = shmem_read_mapping_page_gfp(mapping, i, gfp);
  1892. if (IS_ERR(page)) {
  1893. i915_gem_shrink(dev_priv,
  1894. page_count,
  1895. I915_SHRINK_BOUND |
  1896. I915_SHRINK_UNBOUND |
  1897. I915_SHRINK_PURGEABLE);
  1898. page = shmem_read_mapping_page_gfp(mapping, i, gfp);
  1899. }
  1900. if (IS_ERR(page)) {
  1901. /* We've tried hard to allocate the memory by reaping
  1902. * our own buffer, now let the real VM do its job and
  1903. * go down in flames if truly OOM.
  1904. */
  1905. i915_gem_shrink_all(dev_priv);
  1906. page = shmem_read_mapping_page(mapping, i);
  1907. if (IS_ERR(page)) {
  1908. ret = PTR_ERR(page);
  1909. goto err_pages;
  1910. }
  1911. }
  1912. #ifdef CONFIG_SWIOTLB
  1913. if (swiotlb_nr_tbl()) {
  1914. st->nents++;
  1915. sg_set_page(sg, page, PAGE_SIZE, 0);
  1916. sg = sg_next(sg);
  1917. continue;
  1918. }
  1919. #endif
  1920. if (!i || page_to_pfn(page) != last_pfn + 1) {
  1921. if (i)
  1922. sg = sg_next(sg);
  1923. st->nents++;
  1924. sg_set_page(sg, page, PAGE_SIZE, 0);
  1925. } else {
  1926. sg->length += PAGE_SIZE;
  1927. }
  1928. last_pfn = page_to_pfn(page);
  1929. /* Check that the i965g/gm workaround works. */
  1930. WARN_ON((gfp & __GFP_DMA32) && (last_pfn >= 0x00100000UL));
  1931. }
  1932. #ifdef CONFIG_SWIOTLB
  1933. if (!swiotlb_nr_tbl())
  1934. #endif
  1935. sg_mark_end(sg);
  1936. obj->pages = st;
  1937. ret = i915_gem_gtt_prepare_object(obj);
  1938. if (ret)
  1939. goto err_pages;
  1940. if (i915_gem_object_needs_bit17_swizzle(obj))
  1941. i915_gem_object_do_bit_17_swizzle(obj);
  1942. if (obj->tiling_mode != I915_TILING_NONE &&
  1943. dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES)
  1944. i915_gem_object_pin_pages(obj);
  1945. return 0;
  1946. err_pages:
  1947. sg_mark_end(sg);
  1948. for_each_sg_page(st->sgl, &sg_iter, st->nents, 0)
  1949. put_page(sg_page_iter_page(&sg_iter));
  1950. sg_free_table(st);
  1951. kfree(st);
  1952. /* shmemfs first checks if there is enough memory to allocate the page
  1953. * and reports ENOSPC should there be insufficient, along with the usual
  1954. * ENOMEM for a genuine allocation failure.
  1955. *
  1956. * We use ENOSPC in our driver to mean that we have run out of aperture
  1957. * space and so want to translate the error from shmemfs back to our
  1958. * usual understanding of ENOMEM.
  1959. */
  1960. if (ret == -ENOSPC)
  1961. ret = -ENOMEM;
  1962. return ret;
  1963. }
  1964. /* Ensure that the associated pages are gathered from the backing storage
  1965. * and pinned into our object. i915_gem_object_get_pages() may be called
  1966. * multiple times before they are released by a single call to
  1967. * i915_gem_object_put_pages() - once the pages are no longer referenced
  1968. * either as a result of memory pressure (reaping pages under the shrinker)
  1969. * or as the object is itself released.
  1970. */
  1971. int
  1972. i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
  1973. {
  1974. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  1975. const struct drm_i915_gem_object_ops *ops = obj->ops;
  1976. int ret;
  1977. if (obj->pages)
  1978. return 0;
  1979. if (obj->madv != I915_MADV_WILLNEED) {
  1980. DRM_DEBUG("Attempting to obtain a purgeable object\n");
  1981. return -EFAULT;
  1982. }
  1983. BUG_ON(obj->pages_pin_count);
  1984. ret = ops->get_pages(obj);
  1985. if (ret)
  1986. return ret;
  1987. list_add_tail(&obj->global_list, &dev_priv->mm.unbound_list);
  1988. obj->get_page.sg = obj->pages->sgl;
  1989. obj->get_page.last = 0;
  1990. return 0;
  1991. }
  1992. /* The 'mapping' part of i915_gem_object_pin_map() below */
  1993. static void *i915_gem_object_map(const struct drm_i915_gem_object *obj)
  1994. {
  1995. unsigned long n_pages = obj->base.size >> PAGE_SHIFT;
  1996. struct sg_table *sgt = obj->pages;
  1997. struct sg_page_iter sg_iter;
  1998. struct page *stack_pages[32];
  1999. struct page **pages = stack_pages;
  2000. unsigned long i = 0;
  2001. void *addr;
  2002. /* A single page can always be kmapped */
  2003. if (n_pages == 1)
  2004. return kmap(sg_page(sgt->sgl));
  2005. if (n_pages > ARRAY_SIZE(stack_pages)) {
  2006. /* Too big for stack -- allocate temporary array instead */
  2007. pages = drm_malloc_gfp(n_pages, sizeof(*pages), GFP_TEMPORARY);
  2008. if (!pages)
  2009. return NULL;
  2010. }
  2011. for_each_sg_page(sgt->sgl, &sg_iter, sgt->nents, 0)
  2012. pages[i++] = sg_page_iter_page(&sg_iter);
  2013. /* Check that we have the expected number of pages */
  2014. GEM_BUG_ON(i != n_pages);
  2015. addr = vmap(pages, n_pages, 0, PAGE_KERNEL);
  2016. if (pages != stack_pages)
  2017. drm_free_large(pages);
  2018. return addr;
  2019. }
  2020. /* get, pin, and map the pages of the object into kernel space */
  2021. void *i915_gem_object_pin_map(struct drm_i915_gem_object *obj)
  2022. {
  2023. int ret;
  2024. lockdep_assert_held(&obj->base.dev->struct_mutex);
  2025. ret = i915_gem_object_get_pages(obj);
  2026. if (ret)
  2027. return ERR_PTR(ret);
  2028. i915_gem_object_pin_pages(obj);
  2029. if (!obj->mapping) {
  2030. obj->mapping = i915_gem_object_map(obj);
  2031. if (!obj->mapping) {
  2032. i915_gem_object_unpin_pages(obj);
  2033. return ERR_PTR(-ENOMEM);
  2034. }
  2035. }
  2036. return obj->mapping;
  2037. }
  2038. void i915_vma_move_to_active(struct i915_vma *vma,
  2039. struct drm_i915_gem_request *req)
  2040. {
  2041. struct drm_i915_gem_object *obj = vma->obj;
  2042. struct intel_engine_cs *engine;
  2043. engine = i915_gem_request_get_engine(req);
  2044. /* Add a reference if we're newly entering the active list. */
  2045. if (obj->active == 0)
  2046. drm_gem_object_reference(&obj->base);
  2047. obj->active |= intel_engine_flag(engine);
  2048. list_move_tail(&obj->engine_list[engine->id], &engine->active_list);
  2049. i915_gem_request_assign(&obj->last_read_req[engine->id], req);
  2050. list_move_tail(&vma->vm_link, &vma->vm->active_list);
  2051. }
  2052. static void
  2053. i915_gem_object_retire__write(struct drm_i915_gem_object *obj)
  2054. {
  2055. GEM_BUG_ON(obj->last_write_req == NULL);
  2056. GEM_BUG_ON(!(obj->active & intel_engine_flag(obj->last_write_req->engine)));
  2057. i915_gem_request_assign(&obj->last_write_req, NULL);
  2058. intel_fb_obj_flush(obj, true, ORIGIN_CS);
  2059. }
  2060. static void
  2061. i915_gem_object_retire__read(struct drm_i915_gem_object *obj, int ring)
  2062. {
  2063. struct i915_vma *vma;
  2064. GEM_BUG_ON(obj->last_read_req[ring] == NULL);
  2065. GEM_BUG_ON(!(obj->active & (1 << ring)));
  2066. list_del_init(&obj->engine_list[ring]);
  2067. i915_gem_request_assign(&obj->last_read_req[ring], NULL);
  2068. if (obj->last_write_req && obj->last_write_req->engine->id == ring)
  2069. i915_gem_object_retire__write(obj);
  2070. obj->active &= ~(1 << ring);
  2071. if (obj->active)
  2072. return;
  2073. /* Bump our place on the bound list to keep it roughly in LRU order
  2074. * so that we don't steal from recently used but inactive objects
  2075. * (unless we are forced to ofc!)
  2076. */
  2077. list_move_tail(&obj->global_list,
  2078. &to_i915(obj->base.dev)->mm.bound_list);
  2079. list_for_each_entry(vma, &obj->vma_list, obj_link) {
  2080. if (!list_empty(&vma->vm_link))
  2081. list_move_tail(&vma->vm_link, &vma->vm->inactive_list);
  2082. }
  2083. i915_gem_request_assign(&obj->last_fenced_req, NULL);
  2084. drm_gem_object_unreference(&obj->base);
  2085. }
  2086. static int
  2087. i915_gem_init_seqno(struct drm_i915_private *dev_priv, u32 seqno)
  2088. {
  2089. struct intel_engine_cs *engine;
  2090. int ret;
  2091. /* Carefully retire all requests without writing to the rings */
  2092. for_each_engine(engine, dev_priv) {
  2093. ret = intel_engine_idle(engine);
  2094. if (ret)
  2095. return ret;
  2096. }
  2097. i915_gem_retire_requests(dev_priv);
  2098. /* Finally reset hw state */
  2099. for_each_engine(engine, dev_priv)
  2100. intel_ring_init_seqno(engine, seqno);
  2101. return 0;
  2102. }
  2103. int i915_gem_set_seqno(struct drm_device *dev, u32 seqno)
  2104. {
  2105. struct drm_i915_private *dev_priv = dev->dev_private;
  2106. int ret;
  2107. if (seqno == 0)
  2108. return -EINVAL;
  2109. /* HWS page needs to be set less than what we
  2110. * will inject to ring
  2111. */
  2112. ret = i915_gem_init_seqno(dev_priv, seqno - 1);
  2113. if (ret)
  2114. return ret;
  2115. /* Carefully set the last_seqno value so that wrap
  2116. * detection still works
  2117. */
  2118. dev_priv->next_seqno = seqno;
  2119. dev_priv->last_seqno = seqno - 1;
  2120. if (dev_priv->last_seqno == 0)
  2121. dev_priv->last_seqno--;
  2122. return 0;
  2123. }
  2124. int
  2125. i915_gem_get_seqno(struct drm_i915_private *dev_priv, u32 *seqno)
  2126. {
  2127. /* reserve 0 for non-seqno */
  2128. if (dev_priv->next_seqno == 0) {
  2129. int ret = i915_gem_init_seqno(dev_priv, 0);
  2130. if (ret)
  2131. return ret;
  2132. dev_priv->next_seqno = 1;
  2133. }
  2134. *seqno = dev_priv->last_seqno = dev_priv->next_seqno++;
  2135. return 0;
  2136. }
  2137. /*
  2138. * NB: This function is not allowed to fail. Doing so would mean the the
  2139. * request is not being tracked for completion but the work itself is
  2140. * going to happen on the hardware. This would be a Bad Thing(tm).
  2141. */
  2142. void __i915_add_request(struct drm_i915_gem_request *request,
  2143. struct drm_i915_gem_object *obj,
  2144. bool flush_caches)
  2145. {
  2146. struct intel_engine_cs *engine;
  2147. struct drm_i915_private *dev_priv;
  2148. struct intel_ringbuffer *ringbuf;
  2149. u32 request_start;
  2150. u32 reserved_tail;
  2151. int ret;
  2152. if (WARN_ON(request == NULL))
  2153. return;
  2154. engine = request->engine;
  2155. dev_priv = request->i915;
  2156. ringbuf = request->ringbuf;
  2157. /*
  2158. * To ensure that this call will not fail, space for its emissions
  2159. * should already have been reserved in the ring buffer. Let the ring
  2160. * know that it is time to use that space up.
  2161. */
  2162. request_start = intel_ring_get_tail(ringbuf);
  2163. reserved_tail = request->reserved_space;
  2164. request->reserved_space = 0;
  2165. /*
  2166. * Emit any outstanding flushes - execbuf can fail to emit the flush
  2167. * after having emitted the batchbuffer command. Hence we need to fix
  2168. * things up similar to emitting the lazy request. The difference here
  2169. * is that the flush _must_ happen before the next request, no matter
  2170. * what.
  2171. */
  2172. if (flush_caches) {
  2173. if (i915.enable_execlists)
  2174. ret = logical_ring_flush_all_caches(request);
  2175. else
  2176. ret = intel_ring_flush_all_caches(request);
  2177. /* Not allowed to fail! */
  2178. WARN(ret, "*_ring_flush_all_caches failed: %d!\n", ret);
  2179. }
  2180. trace_i915_gem_request_add(request);
  2181. request->head = request_start;
  2182. /* Whilst this request exists, batch_obj will be on the
  2183. * active_list, and so will hold the active reference. Only when this
  2184. * request is retired will the the batch_obj be moved onto the
  2185. * inactive_list and lose its active reference. Hence we do not need
  2186. * to explicitly hold another reference here.
  2187. */
  2188. request->batch_obj = obj;
  2189. /* Seal the request and mark it as pending execution. Note that
  2190. * we may inspect this state, without holding any locks, during
  2191. * hangcheck. Hence we apply the barrier to ensure that we do not
  2192. * see a more recent value in the hws than we are tracking.
  2193. */
  2194. request->emitted_jiffies = jiffies;
  2195. request->previous_seqno = engine->last_submitted_seqno;
  2196. smp_store_mb(engine->last_submitted_seqno, request->seqno);
  2197. list_add_tail(&request->list, &engine->request_list);
  2198. /* Record the position of the start of the request so that
  2199. * should we detect the updated seqno part-way through the
  2200. * GPU processing the request, we never over-estimate the
  2201. * position of the head.
  2202. */
  2203. request->postfix = intel_ring_get_tail(ringbuf);
  2204. if (i915.enable_execlists)
  2205. ret = engine->emit_request(request);
  2206. else {
  2207. ret = engine->add_request(request);
  2208. request->tail = intel_ring_get_tail(ringbuf);
  2209. }
  2210. /* Not allowed to fail! */
  2211. WARN(ret, "emit|add_request failed: %d!\n", ret);
  2212. i915_queue_hangcheck(engine->i915);
  2213. queue_delayed_work(dev_priv->wq,
  2214. &dev_priv->mm.retire_work,
  2215. round_jiffies_up_relative(HZ));
  2216. intel_mark_busy(dev_priv);
  2217. /* Sanity check that the reserved size was large enough. */
  2218. ret = intel_ring_get_tail(ringbuf) - request_start;
  2219. if (ret < 0)
  2220. ret += ringbuf->size;
  2221. WARN_ONCE(ret > reserved_tail,
  2222. "Not enough space reserved (%d bytes) "
  2223. "for adding the request (%d bytes)\n",
  2224. reserved_tail, ret);
  2225. }
  2226. static bool i915_context_is_banned(struct drm_i915_private *dev_priv,
  2227. const struct intel_context *ctx)
  2228. {
  2229. unsigned long elapsed;
  2230. elapsed = get_seconds() - ctx->hang_stats.guilty_ts;
  2231. if (ctx->hang_stats.banned)
  2232. return true;
  2233. if (ctx->hang_stats.ban_period_seconds &&
  2234. elapsed <= ctx->hang_stats.ban_period_seconds) {
  2235. if (!i915_gem_context_is_default(ctx)) {
  2236. DRM_DEBUG("context hanging too fast, banning!\n");
  2237. return true;
  2238. } else if (i915_stop_ring_allow_ban(dev_priv)) {
  2239. if (i915_stop_ring_allow_warn(dev_priv))
  2240. DRM_ERROR("gpu hanging too fast, banning!\n");
  2241. return true;
  2242. }
  2243. }
  2244. return false;
  2245. }
  2246. static void i915_set_reset_status(struct drm_i915_private *dev_priv,
  2247. struct intel_context *ctx,
  2248. const bool guilty)
  2249. {
  2250. struct i915_ctx_hang_stats *hs;
  2251. if (WARN_ON(!ctx))
  2252. return;
  2253. hs = &ctx->hang_stats;
  2254. if (guilty) {
  2255. hs->banned = i915_context_is_banned(dev_priv, ctx);
  2256. hs->batch_active++;
  2257. hs->guilty_ts = get_seconds();
  2258. } else {
  2259. hs->batch_pending++;
  2260. }
  2261. }
  2262. void i915_gem_request_free(struct kref *req_ref)
  2263. {
  2264. struct drm_i915_gem_request *req = container_of(req_ref,
  2265. typeof(*req), ref);
  2266. kmem_cache_free(req->i915->requests, req);
  2267. }
  2268. static inline int
  2269. __i915_gem_request_alloc(struct intel_engine_cs *engine,
  2270. struct intel_context *ctx,
  2271. struct drm_i915_gem_request **req_out)
  2272. {
  2273. struct drm_i915_private *dev_priv = engine->i915;
  2274. unsigned reset_counter = i915_reset_counter(&dev_priv->gpu_error);
  2275. struct drm_i915_gem_request *req;
  2276. int ret;
  2277. if (!req_out)
  2278. return -EINVAL;
  2279. *req_out = NULL;
  2280. /* ABI: Before userspace accesses the GPU (e.g. execbuffer), report
  2281. * EIO if the GPU is already wedged, or EAGAIN to drop the struct_mutex
  2282. * and restart.
  2283. */
  2284. ret = i915_gem_check_wedge(reset_counter, dev_priv->mm.interruptible);
  2285. if (ret)
  2286. return ret;
  2287. req = kmem_cache_zalloc(dev_priv->requests, GFP_KERNEL);
  2288. if (req == NULL)
  2289. return -ENOMEM;
  2290. ret = i915_gem_get_seqno(engine->i915, &req->seqno);
  2291. if (ret)
  2292. goto err;
  2293. kref_init(&req->ref);
  2294. req->i915 = dev_priv;
  2295. req->engine = engine;
  2296. req->reset_counter = reset_counter;
  2297. req->ctx = ctx;
  2298. i915_gem_context_reference(req->ctx);
  2299. /*
  2300. * Reserve space in the ring buffer for all the commands required to
  2301. * eventually emit this request. This is to guarantee that the
  2302. * i915_add_request() call can't fail. Note that the reserve may need
  2303. * to be redone if the request is not actually submitted straight
  2304. * away, e.g. because a GPU scheduler has deferred it.
  2305. */
  2306. req->reserved_space = MIN_SPACE_FOR_ADD_REQUEST;
  2307. if (i915.enable_execlists)
  2308. ret = intel_logical_ring_alloc_request_extras(req);
  2309. else
  2310. ret = intel_ring_alloc_request_extras(req);
  2311. if (ret)
  2312. goto err_ctx;
  2313. *req_out = req;
  2314. return 0;
  2315. err_ctx:
  2316. i915_gem_context_unreference(ctx);
  2317. err:
  2318. kmem_cache_free(dev_priv->requests, req);
  2319. return ret;
  2320. }
  2321. /**
  2322. * i915_gem_request_alloc - allocate a request structure
  2323. *
  2324. * @engine: engine that we wish to issue the request on.
  2325. * @ctx: context that the request will be associated with.
  2326. * This can be NULL if the request is not directly related to
  2327. * any specific user context, in which case this function will
  2328. * choose an appropriate context to use.
  2329. *
  2330. * Returns a pointer to the allocated request if successful,
  2331. * or an error code if not.
  2332. */
  2333. struct drm_i915_gem_request *
  2334. i915_gem_request_alloc(struct intel_engine_cs *engine,
  2335. struct intel_context *ctx)
  2336. {
  2337. struct drm_i915_gem_request *req;
  2338. int err;
  2339. if (ctx == NULL)
  2340. ctx = engine->i915->kernel_context;
  2341. err = __i915_gem_request_alloc(engine, ctx, &req);
  2342. return err ? ERR_PTR(err) : req;
  2343. }
  2344. struct drm_i915_gem_request *
  2345. i915_gem_find_active_request(struct intel_engine_cs *engine)
  2346. {
  2347. struct drm_i915_gem_request *request;
  2348. list_for_each_entry(request, &engine->request_list, list) {
  2349. if (i915_gem_request_completed(request, false))
  2350. continue;
  2351. return request;
  2352. }
  2353. return NULL;
  2354. }
  2355. static void i915_gem_reset_engine_status(struct drm_i915_private *dev_priv,
  2356. struct intel_engine_cs *engine)
  2357. {
  2358. struct drm_i915_gem_request *request;
  2359. bool ring_hung;
  2360. request = i915_gem_find_active_request(engine);
  2361. if (request == NULL)
  2362. return;
  2363. ring_hung = engine->hangcheck.score >= HANGCHECK_SCORE_RING_HUNG;
  2364. i915_set_reset_status(dev_priv, request->ctx, ring_hung);
  2365. list_for_each_entry_continue(request, &engine->request_list, list)
  2366. i915_set_reset_status(dev_priv, request->ctx, false);
  2367. }
  2368. static void i915_gem_reset_engine_cleanup(struct drm_i915_private *dev_priv,
  2369. struct intel_engine_cs *engine)
  2370. {
  2371. struct intel_ringbuffer *buffer;
  2372. while (!list_empty(&engine->active_list)) {
  2373. struct drm_i915_gem_object *obj;
  2374. obj = list_first_entry(&engine->active_list,
  2375. struct drm_i915_gem_object,
  2376. engine_list[engine->id]);
  2377. i915_gem_object_retire__read(obj, engine->id);
  2378. }
  2379. /*
  2380. * Clear the execlists queue up before freeing the requests, as those
  2381. * are the ones that keep the context and ringbuffer backing objects
  2382. * pinned in place.
  2383. */
  2384. if (i915.enable_execlists) {
  2385. /* Ensure irq handler finishes or is cancelled. */
  2386. tasklet_kill(&engine->irq_tasklet);
  2387. intel_execlists_cancel_requests(engine);
  2388. }
  2389. /*
  2390. * We must free the requests after all the corresponding objects have
  2391. * been moved off active lists. Which is the same order as the normal
  2392. * retire_requests function does. This is important if object hold
  2393. * implicit references on things like e.g. ppgtt address spaces through
  2394. * the request.
  2395. */
  2396. while (!list_empty(&engine->request_list)) {
  2397. struct drm_i915_gem_request *request;
  2398. request = list_first_entry(&engine->request_list,
  2399. struct drm_i915_gem_request,
  2400. list);
  2401. i915_gem_request_retire(request);
  2402. }
  2403. /* Having flushed all requests from all queues, we know that all
  2404. * ringbuffers must now be empty. However, since we do not reclaim
  2405. * all space when retiring the request (to prevent HEADs colliding
  2406. * with rapid ringbuffer wraparound) the amount of available space
  2407. * upon reset is less than when we start. Do one more pass over
  2408. * all the ringbuffers to reset last_retired_head.
  2409. */
  2410. list_for_each_entry(buffer, &engine->buffers, link) {
  2411. buffer->last_retired_head = buffer->tail;
  2412. intel_ring_update_space(buffer);
  2413. }
  2414. intel_ring_init_seqno(engine, engine->last_submitted_seqno);
  2415. }
  2416. void i915_gem_reset(struct drm_device *dev)
  2417. {
  2418. struct drm_i915_private *dev_priv = dev->dev_private;
  2419. struct intel_engine_cs *engine;
  2420. /*
  2421. * Before we free the objects from the requests, we need to inspect
  2422. * them for finding the guilty party. As the requests only borrow
  2423. * their reference to the objects, the inspection must be done first.
  2424. */
  2425. for_each_engine(engine, dev_priv)
  2426. i915_gem_reset_engine_status(dev_priv, engine);
  2427. for_each_engine(engine, dev_priv)
  2428. i915_gem_reset_engine_cleanup(dev_priv, engine);
  2429. i915_gem_context_reset(dev);
  2430. i915_gem_restore_fences(dev);
  2431. WARN_ON(i915_verify_lists(dev));
  2432. }
  2433. /**
  2434. * This function clears the request list as sequence numbers are passed.
  2435. */
  2436. void
  2437. i915_gem_retire_requests_ring(struct intel_engine_cs *engine)
  2438. {
  2439. WARN_ON(i915_verify_lists(engine->dev));
  2440. /* Retire requests first as we use it above for the early return.
  2441. * If we retire requests last, we may use a later seqno and so clear
  2442. * the requests lists without clearing the active list, leading to
  2443. * confusion.
  2444. */
  2445. while (!list_empty(&engine->request_list)) {
  2446. struct drm_i915_gem_request *request;
  2447. request = list_first_entry(&engine->request_list,
  2448. struct drm_i915_gem_request,
  2449. list);
  2450. if (!i915_gem_request_completed(request, true))
  2451. break;
  2452. i915_gem_request_retire(request);
  2453. }
  2454. /* Move any buffers on the active list that are no longer referenced
  2455. * by the ringbuffer to the flushing/inactive lists as appropriate,
  2456. * before we free the context associated with the requests.
  2457. */
  2458. while (!list_empty(&engine->active_list)) {
  2459. struct drm_i915_gem_object *obj;
  2460. obj = list_first_entry(&engine->active_list,
  2461. struct drm_i915_gem_object,
  2462. engine_list[engine->id]);
  2463. if (!list_empty(&obj->last_read_req[engine->id]->list))
  2464. break;
  2465. i915_gem_object_retire__read(obj, engine->id);
  2466. }
  2467. if (unlikely(engine->trace_irq_req &&
  2468. i915_gem_request_completed(engine->trace_irq_req, true))) {
  2469. engine->irq_put(engine);
  2470. i915_gem_request_assign(&engine->trace_irq_req, NULL);
  2471. }
  2472. WARN_ON(i915_verify_lists(engine->dev));
  2473. }
  2474. bool
  2475. i915_gem_retire_requests(struct drm_i915_private *dev_priv)
  2476. {
  2477. struct intel_engine_cs *engine;
  2478. bool idle = true;
  2479. for_each_engine(engine, dev_priv) {
  2480. i915_gem_retire_requests_ring(engine);
  2481. idle &= list_empty(&engine->request_list);
  2482. if (i915.enable_execlists) {
  2483. spin_lock_bh(&engine->execlist_lock);
  2484. idle &= list_empty(&engine->execlist_queue);
  2485. spin_unlock_bh(&engine->execlist_lock);
  2486. }
  2487. }
  2488. if (idle)
  2489. mod_delayed_work(dev_priv->wq,
  2490. &dev_priv->mm.idle_work,
  2491. msecs_to_jiffies(100));
  2492. return idle;
  2493. }
  2494. static void
  2495. i915_gem_retire_work_handler(struct work_struct *work)
  2496. {
  2497. struct drm_i915_private *dev_priv =
  2498. container_of(work, typeof(*dev_priv), mm.retire_work.work);
  2499. struct drm_device *dev = dev_priv->dev;
  2500. bool idle;
  2501. /* Come back later if the device is busy... */
  2502. idle = false;
  2503. if (mutex_trylock(&dev->struct_mutex)) {
  2504. idle = i915_gem_retire_requests(dev_priv);
  2505. mutex_unlock(&dev->struct_mutex);
  2506. }
  2507. if (!idle)
  2508. queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work,
  2509. round_jiffies_up_relative(HZ));
  2510. }
  2511. static void
  2512. i915_gem_idle_work_handler(struct work_struct *work)
  2513. {
  2514. struct drm_i915_private *dev_priv =
  2515. container_of(work, typeof(*dev_priv), mm.idle_work.work);
  2516. struct drm_device *dev = dev_priv->dev;
  2517. struct intel_engine_cs *engine;
  2518. for_each_engine(engine, dev_priv)
  2519. if (!list_empty(&engine->request_list))
  2520. return;
  2521. /* we probably should sync with hangcheck here, using cancel_work_sync.
  2522. * Also locking seems to be fubar here, engine->request_list is protected
  2523. * by dev->struct_mutex. */
  2524. intel_mark_idle(dev_priv);
  2525. if (mutex_trylock(&dev->struct_mutex)) {
  2526. for_each_engine(engine, dev_priv)
  2527. i915_gem_batch_pool_fini(&engine->batch_pool);
  2528. mutex_unlock(&dev->struct_mutex);
  2529. }
  2530. }
  2531. /**
  2532. * Ensures that an object will eventually get non-busy by flushing any required
  2533. * write domains, emitting any outstanding lazy request and retiring and
  2534. * completed requests.
  2535. */
  2536. static int
  2537. i915_gem_object_flush_active(struct drm_i915_gem_object *obj)
  2538. {
  2539. int i;
  2540. if (!obj->active)
  2541. return 0;
  2542. for (i = 0; i < I915_NUM_ENGINES; i++) {
  2543. struct drm_i915_gem_request *req;
  2544. req = obj->last_read_req[i];
  2545. if (req == NULL)
  2546. continue;
  2547. if (i915_gem_request_completed(req, true))
  2548. i915_gem_object_retire__read(obj, i);
  2549. }
  2550. return 0;
  2551. }
  2552. /**
  2553. * i915_gem_wait_ioctl - implements DRM_IOCTL_I915_GEM_WAIT
  2554. * @DRM_IOCTL_ARGS: standard ioctl arguments
  2555. *
  2556. * Returns 0 if successful, else an error is returned with the remaining time in
  2557. * the timeout parameter.
  2558. * -ETIME: object is still busy after timeout
  2559. * -ERESTARTSYS: signal interrupted the wait
  2560. * -ENONENT: object doesn't exist
  2561. * Also possible, but rare:
  2562. * -EAGAIN: GPU wedged
  2563. * -ENOMEM: damn
  2564. * -ENODEV: Internal IRQ fail
  2565. * -E?: The add request failed
  2566. *
  2567. * The wait ioctl with a timeout of 0 reimplements the busy ioctl. With any
  2568. * non-zero timeout parameter the wait ioctl will wait for the given number of
  2569. * nanoseconds on an object becoming unbusy. Since the wait itself does so
  2570. * without holding struct_mutex the object may become re-busied before this
  2571. * function completes. A similar but shorter * race condition exists in the busy
  2572. * ioctl
  2573. */
  2574. int
  2575. i915_gem_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
  2576. {
  2577. struct drm_i915_gem_wait *args = data;
  2578. struct drm_i915_gem_object *obj;
  2579. struct drm_i915_gem_request *req[I915_NUM_ENGINES];
  2580. int i, n = 0;
  2581. int ret;
  2582. if (args->flags != 0)
  2583. return -EINVAL;
  2584. ret = i915_mutex_lock_interruptible(dev);
  2585. if (ret)
  2586. return ret;
  2587. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->bo_handle));
  2588. if (&obj->base == NULL) {
  2589. mutex_unlock(&dev->struct_mutex);
  2590. return -ENOENT;
  2591. }
  2592. /* Need to make sure the object gets inactive eventually. */
  2593. ret = i915_gem_object_flush_active(obj);
  2594. if (ret)
  2595. goto out;
  2596. if (!obj->active)
  2597. goto out;
  2598. /* Do this after OLR check to make sure we make forward progress polling
  2599. * on this IOCTL with a timeout == 0 (like busy ioctl)
  2600. */
  2601. if (args->timeout_ns == 0) {
  2602. ret = -ETIME;
  2603. goto out;
  2604. }
  2605. drm_gem_object_unreference(&obj->base);
  2606. for (i = 0; i < I915_NUM_ENGINES; i++) {
  2607. if (obj->last_read_req[i] == NULL)
  2608. continue;
  2609. req[n++] = i915_gem_request_reference(obj->last_read_req[i]);
  2610. }
  2611. mutex_unlock(&dev->struct_mutex);
  2612. for (i = 0; i < n; i++) {
  2613. if (ret == 0)
  2614. ret = __i915_wait_request(req[i], true,
  2615. args->timeout_ns > 0 ? &args->timeout_ns : NULL,
  2616. to_rps_client(file));
  2617. i915_gem_request_unreference(req[i]);
  2618. }
  2619. return ret;
  2620. out:
  2621. drm_gem_object_unreference(&obj->base);
  2622. mutex_unlock(&dev->struct_mutex);
  2623. return ret;
  2624. }
  2625. static int
  2626. __i915_gem_object_sync(struct drm_i915_gem_object *obj,
  2627. struct intel_engine_cs *to,
  2628. struct drm_i915_gem_request *from_req,
  2629. struct drm_i915_gem_request **to_req)
  2630. {
  2631. struct intel_engine_cs *from;
  2632. int ret;
  2633. from = i915_gem_request_get_engine(from_req);
  2634. if (to == from)
  2635. return 0;
  2636. if (i915_gem_request_completed(from_req, true))
  2637. return 0;
  2638. if (!i915_semaphore_is_enabled(to_i915(obj->base.dev))) {
  2639. struct drm_i915_private *i915 = to_i915(obj->base.dev);
  2640. ret = __i915_wait_request(from_req,
  2641. i915->mm.interruptible,
  2642. NULL,
  2643. &i915->rps.semaphores);
  2644. if (ret)
  2645. return ret;
  2646. i915_gem_object_retire_request(obj, from_req);
  2647. } else {
  2648. int idx = intel_ring_sync_index(from, to);
  2649. u32 seqno = i915_gem_request_get_seqno(from_req);
  2650. WARN_ON(!to_req);
  2651. if (seqno <= from->semaphore.sync_seqno[idx])
  2652. return 0;
  2653. if (*to_req == NULL) {
  2654. struct drm_i915_gem_request *req;
  2655. req = i915_gem_request_alloc(to, NULL);
  2656. if (IS_ERR(req))
  2657. return PTR_ERR(req);
  2658. *to_req = req;
  2659. }
  2660. trace_i915_gem_ring_sync_to(*to_req, from, from_req);
  2661. ret = to->semaphore.sync_to(*to_req, from, seqno);
  2662. if (ret)
  2663. return ret;
  2664. /* We use last_read_req because sync_to()
  2665. * might have just caused seqno wrap under
  2666. * the radar.
  2667. */
  2668. from->semaphore.sync_seqno[idx] =
  2669. i915_gem_request_get_seqno(obj->last_read_req[from->id]);
  2670. }
  2671. return 0;
  2672. }
  2673. /**
  2674. * i915_gem_object_sync - sync an object to a ring.
  2675. *
  2676. * @obj: object which may be in use on another ring.
  2677. * @to: ring we wish to use the object on. May be NULL.
  2678. * @to_req: request we wish to use the object for. See below.
  2679. * This will be allocated and returned if a request is
  2680. * required but not passed in.
  2681. *
  2682. * This code is meant to abstract object synchronization with the GPU.
  2683. * Calling with NULL implies synchronizing the object with the CPU
  2684. * rather than a particular GPU ring. Conceptually we serialise writes
  2685. * between engines inside the GPU. We only allow one engine to write
  2686. * into a buffer at any time, but multiple readers. To ensure each has
  2687. * a coherent view of memory, we must:
  2688. *
  2689. * - If there is an outstanding write request to the object, the new
  2690. * request must wait for it to complete (either CPU or in hw, requests
  2691. * on the same ring will be naturally ordered).
  2692. *
  2693. * - If we are a write request (pending_write_domain is set), the new
  2694. * request must wait for outstanding read requests to complete.
  2695. *
  2696. * For CPU synchronisation (NULL to) no request is required. For syncing with
  2697. * rings to_req must be non-NULL. However, a request does not have to be
  2698. * pre-allocated. If *to_req is NULL and sync commands will be emitted then a
  2699. * request will be allocated automatically and returned through *to_req. Note
  2700. * that it is not guaranteed that commands will be emitted (because the system
  2701. * might already be idle). Hence there is no need to create a request that
  2702. * might never have any work submitted. Note further that if a request is
  2703. * returned in *to_req, it is the responsibility of the caller to submit
  2704. * that request (after potentially adding more work to it).
  2705. *
  2706. * Returns 0 if successful, else propagates up the lower layer error.
  2707. */
  2708. int
  2709. i915_gem_object_sync(struct drm_i915_gem_object *obj,
  2710. struct intel_engine_cs *to,
  2711. struct drm_i915_gem_request **to_req)
  2712. {
  2713. const bool readonly = obj->base.pending_write_domain == 0;
  2714. struct drm_i915_gem_request *req[I915_NUM_ENGINES];
  2715. int ret, i, n;
  2716. if (!obj->active)
  2717. return 0;
  2718. if (to == NULL)
  2719. return i915_gem_object_wait_rendering(obj, readonly);
  2720. n = 0;
  2721. if (readonly) {
  2722. if (obj->last_write_req)
  2723. req[n++] = obj->last_write_req;
  2724. } else {
  2725. for (i = 0; i < I915_NUM_ENGINES; i++)
  2726. if (obj->last_read_req[i])
  2727. req[n++] = obj->last_read_req[i];
  2728. }
  2729. for (i = 0; i < n; i++) {
  2730. ret = __i915_gem_object_sync(obj, to, req[i], to_req);
  2731. if (ret)
  2732. return ret;
  2733. }
  2734. return 0;
  2735. }
  2736. static void i915_gem_object_finish_gtt(struct drm_i915_gem_object *obj)
  2737. {
  2738. u32 old_write_domain, old_read_domains;
  2739. /* Force a pagefault for domain tracking on next user access */
  2740. i915_gem_release_mmap(obj);
  2741. if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0)
  2742. return;
  2743. old_read_domains = obj->base.read_domains;
  2744. old_write_domain = obj->base.write_domain;
  2745. obj->base.read_domains &= ~I915_GEM_DOMAIN_GTT;
  2746. obj->base.write_domain &= ~I915_GEM_DOMAIN_GTT;
  2747. trace_i915_gem_object_change_domain(obj,
  2748. old_read_domains,
  2749. old_write_domain);
  2750. }
  2751. static void __i915_vma_iounmap(struct i915_vma *vma)
  2752. {
  2753. GEM_BUG_ON(vma->pin_count);
  2754. if (vma->iomap == NULL)
  2755. return;
  2756. io_mapping_unmap(vma->iomap);
  2757. vma->iomap = NULL;
  2758. }
  2759. static int __i915_vma_unbind(struct i915_vma *vma, bool wait)
  2760. {
  2761. struct drm_i915_gem_object *obj = vma->obj;
  2762. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  2763. int ret;
  2764. if (list_empty(&vma->obj_link))
  2765. return 0;
  2766. if (!drm_mm_node_allocated(&vma->node)) {
  2767. i915_gem_vma_destroy(vma);
  2768. return 0;
  2769. }
  2770. if (vma->pin_count)
  2771. return -EBUSY;
  2772. BUG_ON(obj->pages == NULL);
  2773. if (wait) {
  2774. ret = i915_gem_object_wait_rendering(obj, false);
  2775. if (ret)
  2776. return ret;
  2777. }
  2778. if (vma->is_ggtt && vma->ggtt_view.type == I915_GGTT_VIEW_NORMAL) {
  2779. i915_gem_object_finish_gtt(obj);
  2780. /* release the fence reg _after_ flushing */
  2781. ret = i915_gem_object_put_fence(obj);
  2782. if (ret)
  2783. return ret;
  2784. __i915_vma_iounmap(vma);
  2785. }
  2786. trace_i915_vma_unbind(vma);
  2787. vma->vm->unbind_vma(vma);
  2788. vma->bound = 0;
  2789. list_del_init(&vma->vm_link);
  2790. if (vma->is_ggtt) {
  2791. if (vma->ggtt_view.type == I915_GGTT_VIEW_NORMAL) {
  2792. obj->map_and_fenceable = false;
  2793. } else if (vma->ggtt_view.pages) {
  2794. sg_free_table(vma->ggtt_view.pages);
  2795. kfree(vma->ggtt_view.pages);
  2796. }
  2797. vma->ggtt_view.pages = NULL;
  2798. }
  2799. drm_mm_remove_node(&vma->node);
  2800. i915_gem_vma_destroy(vma);
  2801. /* Since the unbound list is global, only move to that list if
  2802. * no more VMAs exist. */
  2803. if (list_empty(&obj->vma_list))
  2804. list_move_tail(&obj->global_list, &dev_priv->mm.unbound_list);
  2805. /* And finally now the object is completely decoupled from this vma,
  2806. * we can drop its hold on the backing storage and allow it to be
  2807. * reaped by the shrinker.
  2808. */
  2809. i915_gem_object_unpin_pages(obj);
  2810. return 0;
  2811. }
  2812. int i915_vma_unbind(struct i915_vma *vma)
  2813. {
  2814. return __i915_vma_unbind(vma, true);
  2815. }
  2816. int __i915_vma_unbind_no_wait(struct i915_vma *vma)
  2817. {
  2818. return __i915_vma_unbind(vma, false);
  2819. }
  2820. int i915_gpu_idle(struct drm_device *dev)
  2821. {
  2822. struct drm_i915_private *dev_priv = dev->dev_private;
  2823. struct intel_engine_cs *engine;
  2824. int ret;
  2825. /* Flush everything onto the inactive list. */
  2826. for_each_engine(engine, dev_priv) {
  2827. if (!i915.enable_execlists) {
  2828. struct drm_i915_gem_request *req;
  2829. req = i915_gem_request_alloc(engine, NULL);
  2830. if (IS_ERR(req))
  2831. return PTR_ERR(req);
  2832. ret = i915_switch_context(req);
  2833. i915_add_request_no_flush(req);
  2834. if (ret)
  2835. return ret;
  2836. }
  2837. ret = intel_engine_idle(engine);
  2838. if (ret)
  2839. return ret;
  2840. }
  2841. WARN_ON(i915_verify_lists(dev));
  2842. return 0;
  2843. }
  2844. static bool i915_gem_valid_gtt_space(struct i915_vma *vma,
  2845. unsigned long cache_level)
  2846. {
  2847. struct drm_mm_node *gtt_space = &vma->node;
  2848. struct drm_mm_node *other;
  2849. /*
  2850. * On some machines we have to be careful when putting differing types
  2851. * of snoopable memory together to avoid the prefetcher crossing memory
  2852. * domains and dying. During vm initialisation, we decide whether or not
  2853. * these constraints apply and set the drm_mm.color_adjust
  2854. * appropriately.
  2855. */
  2856. if (vma->vm->mm.color_adjust == NULL)
  2857. return true;
  2858. if (!drm_mm_node_allocated(gtt_space))
  2859. return true;
  2860. if (list_empty(&gtt_space->node_list))
  2861. return true;
  2862. other = list_entry(gtt_space->node_list.prev, struct drm_mm_node, node_list);
  2863. if (other->allocated && !other->hole_follows && other->color != cache_level)
  2864. return false;
  2865. other = list_entry(gtt_space->node_list.next, struct drm_mm_node, node_list);
  2866. if (other->allocated && !gtt_space->hole_follows && other->color != cache_level)
  2867. return false;
  2868. return true;
  2869. }
  2870. /**
  2871. * Finds free space in the GTT aperture and binds the object or a view of it
  2872. * there.
  2873. */
  2874. static struct i915_vma *
  2875. i915_gem_object_bind_to_vm(struct drm_i915_gem_object *obj,
  2876. struct i915_address_space *vm,
  2877. const struct i915_ggtt_view *ggtt_view,
  2878. unsigned alignment,
  2879. uint64_t flags)
  2880. {
  2881. struct drm_device *dev = obj->base.dev;
  2882. struct drm_i915_private *dev_priv = to_i915(dev);
  2883. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  2884. u32 fence_alignment, unfenced_alignment;
  2885. u32 search_flag, alloc_flag;
  2886. u64 start, end;
  2887. u64 size, fence_size;
  2888. struct i915_vma *vma;
  2889. int ret;
  2890. if (i915_is_ggtt(vm)) {
  2891. u32 view_size;
  2892. if (WARN_ON(!ggtt_view))
  2893. return ERR_PTR(-EINVAL);
  2894. view_size = i915_ggtt_view_size(obj, ggtt_view);
  2895. fence_size = i915_gem_get_gtt_size(dev,
  2896. view_size,
  2897. obj->tiling_mode);
  2898. fence_alignment = i915_gem_get_gtt_alignment(dev,
  2899. view_size,
  2900. obj->tiling_mode,
  2901. true);
  2902. unfenced_alignment = i915_gem_get_gtt_alignment(dev,
  2903. view_size,
  2904. obj->tiling_mode,
  2905. false);
  2906. size = flags & PIN_MAPPABLE ? fence_size : view_size;
  2907. } else {
  2908. fence_size = i915_gem_get_gtt_size(dev,
  2909. obj->base.size,
  2910. obj->tiling_mode);
  2911. fence_alignment = i915_gem_get_gtt_alignment(dev,
  2912. obj->base.size,
  2913. obj->tiling_mode,
  2914. true);
  2915. unfenced_alignment =
  2916. i915_gem_get_gtt_alignment(dev,
  2917. obj->base.size,
  2918. obj->tiling_mode,
  2919. false);
  2920. size = flags & PIN_MAPPABLE ? fence_size : obj->base.size;
  2921. }
  2922. start = flags & PIN_OFFSET_BIAS ? flags & PIN_OFFSET_MASK : 0;
  2923. end = vm->total;
  2924. if (flags & PIN_MAPPABLE)
  2925. end = min_t(u64, end, ggtt->mappable_end);
  2926. if (flags & PIN_ZONE_4G)
  2927. end = min_t(u64, end, (1ULL << 32) - PAGE_SIZE);
  2928. if (alignment == 0)
  2929. alignment = flags & PIN_MAPPABLE ? fence_alignment :
  2930. unfenced_alignment;
  2931. if (flags & PIN_MAPPABLE && alignment & (fence_alignment - 1)) {
  2932. DRM_DEBUG("Invalid object (view type=%u) alignment requested %u\n",
  2933. ggtt_view ? ggtt_view->type : 0,
  2934. alignment);
  2935. return ERR_PTR(-EINVAL);
  2936. }
  2937. /* If binding the object/GGTT view requires more space than the entire
  2938. * aperture has, reject it early before evicting everything in a vain
  2939. * attempt to find space.
  2940. */
  2941. if (size > end) {
  2942. DRM_DEBUG("Attempting to bind an object (view type=%u) larger than the aperture: size=%llu > %s aperture=%llu\n",
  2943. ggtt_view ? ggtt_view->type : 0,
  2944. size,
  2945. flags & PIN_MAPPABLE ? "mappable" : "total",
  2946. end);
  2947. return ERR_PTR(-E2BIG);
  2948. }
  2949. ret = i915_gem_object_get_pages(obj);
  2950. if (ret)
  2951. return ERR_PTR(ret);
  2952. i915_gem_object_pin_pages(obj);
  2953. vma = ggtt_view ? i915_gem_obj_lookup_or_create_ggtt_vma(obj, ggtt_view) :
  2954. i915_gem_obj_lookup_or_create_vma(obj, vm);
  2955. if (IS_ERR(vma))
  2956. goto err_unpin;
  2957. if (flags & PIN_OFFSET_FIXED) {
  2958. uint64_t offset = flags & PIN_OFFSET_MASK;
  2959. if (offset & (alignment - 1) || offset + size > end) {
  2960. ret = -EINVAL;
  2961. goto err_free_vma;
  2962. }
  2963. vma->node.start = offset;
  2964. vma->node.size = size;
  2965. vma->node.color = obj->cache_level;
  2966. ret = drm_mm_reserve_node(&vm->mm, &vma->node);
  2967. if (ret) {
  2968. ret = i915_gem_evict_for_vma(vma);
  2969. if (ret == 0)
  2970. ret = drm_mm_reserve_node(&vm->mm, &vma->node);
  2971. }
  2972. if (ret)
  2973. goto err_free_vma;
  2974. } else {
  2975. if (flags & PIN_HIGH) {
  2976. search_flag = DRM_MM_SEARCH_BELOW;
  2977. alloc_flag = DRM_MM_CREATE_TOP;
  2978. } else {
  2979. search_flag = DRM_MM_SEARCH_DEFAULT;
  2980. alloc_flag = DRM_MM_CREATE_DEFAULT;
  2981. }
  2982. search_free:
  2983. ret = drm_mm_insert_node_in_range_generic(&vm->mm, &vma->node,
  2984. size, alignment,
  2985. obj->cache_level,
  2986. start, end,
  2987. search_flag,
  2988. alloc_flag);
  2989. if (ret) {
  2990. ret = i915_gem_evict_something(dev, vm, size, alignment,
  2991. obj->cache_level,
  2992. start, end,
  2993. flags);
  2994. if (ret == 0)
  2995. goto search_free;
  2996. goto err_free_vma;
  2997. }
  2998. }
  2999. if (WARN_ON(!i915_gem_valid_gtt_space(vma, obj->cache_level))) {
  3000. ret = -EINVAL;
  3001. goto err_remove_node;
  3002. }
  3003. trace_i915_vma_bind(vma, flags);
  3004. ret = i915_vma_bind(vma, obj->cache_level, flags);
  3005. if (ret)
  3006. goto err_remove_node;
  3007. list_move_tail(&obj->global_list, &dev_priv->mm.bound_list);
  3008. list_add_tail(&vma->vm_link, &vm->inactive_list);
  3009. return vma;
  3010. err_remove_node:
  3011. drm_mm_remove_node(&vma->node);
  3012. err_free_vma:
  3013. i915_gem_vma_destroy(vma);
  3014. vma = ERR_PTR(ret);
  3015. err_unpin:
  3016. i915_gem_object_unpin_pages(obj);
  3017. return vma;
  3018. }
  3019. bool
  3020. i915_gem_clflush_object(struct drm_i915_gem_object *obj,
  3021. bool force)
  3022. {
  3023. /* If we don't have a page list set up, then we're not pinned
  3024. * to GPU, and we can ignore the cache flush because it'll happen
  3025. * again at bind time.
  3026. */
  3027. if (obj->pages == NULL)
  3028. return false;
  3029. /*
  3030. * Stolen memory is always coherent with the GPU as it is explicitly
  3031. * marked as wc by the system, or the system is cache-coherent.
  3032. */
  3033. if (obj->stolen || obj->phys_handle)
  3034. return false;
  3035. /* If the GPU is snooping the contents of the CPU cache,
  3036. * we do not need to manually clear the CPU cache lines. However,
  3037. * the caches are only snooped when the render cache is
  3038. * flushed/invalidated. As we always have to emit invalidations
  3039. * and flushes when moving into and out of the RENDER domain, correct
  3040. * snooping behaviour occurs naturally as the result of our domain
  3041. * tracking.
  3042. */
  3043. if (!force && cpu_cache_is_coherent(obj->base.dev, obj->cache_level)) {
  3044. obj->cache_dirty = true;
  3045. return false;
  3046. }
  3047. trace_i915_gem_object_clflush(obj);
  3048. drm_clflush_sg(obj->pages);
  3049. obj->cache_dirty = false;
  3050. return true;
  3051. }
  3052. /** Flushes the GTT write domain for the object if it's dirty. */
  3053. static void
  3054. i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj)
  3055. {
  3056. uint32_t old_write_domain;
  3057. if (obj->base.write_domain != I915_GEM_DOMAIN_GTT)
  3058. return;
  3059. /* No actual flushing is required for the GTT write domain. Writes
  3060. * to it immediately go to main memory as far as we know, so there's
  3061. * no chipset flush. It also doesn't land in render cache.
  3062. *
  3063. * However, we do have to enforce the order so that all writes through
  3064. * the GTT land before any writes to the device, such as updates to
  3065. * the GATT itself.
  3066. */
  3067. wmb();
  3068. old_write_domain = obj->base.write_domain;
  3069. obj->base.write_domain = 0;
  3070. intel_fb_obj_flush(obj, false, ORIGIN_GTT);
  3071. trace_i915_gem_object_change_domain(obj,
  3072. obj->base.read_domains,
  3073. old_write_domain);
  3074. }
  3075. /** Flushes the CPU write domain for the object if it's dirty. */
  3076. static void
  3077. i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj)
  3078. {
  3079. uint32_t old_write_domain;
  3080. if (obj->base.write_domain != I915_GEM_DOMAIN_CPU)
  3081. return;
  3082. if (i915_gem_clflush_object(obj, obj->pin_display))
  3083. i915_gem_chipset_flush(to_i915(obj->base.dev));
  3084. old_write_domain = obj->base.write_domain;
  3085. obj->base.write_domain = 0;
  3086. intel_fb_obj_flush(obj, false, ORIGIN_CPU);
  3087. trace_i915_gem_object_change_domain(obj,
  3088. obj->base.read_domains,
  3089. old_write_domain);
  3090. }
  3091. /**
  3092. * Moves a single object to the GTT read, and possibly write domain.
  3093. *
  3094. * This function returns when the move is complete, including waiting on
  3095. * flushes to occur.
  3096. */
  3097. int
  3098. i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write)
  3099. {
  3100. struct drm_device *dev = obj->base.dev;
  3101. struct drm_i915_private *dev_priv = to_i915(dev);
  3102. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  3103. uint32_t old_write_domain, old_read_domains;
  3104. struct i915_vma *vma;
  3105. int ret;
  3106. if (obj->base.write_domain == I915_GEM_DOMAIN_GTT)
  3107. return 0;
  3108. ret = i915_gem_object_wait_rendering(obj, !write);
  3109. if (ret)
  3110. return ret;
  3111. /* Flush and acquire obj->pages so that we are coherent through
  3112. * direct access in memory with previous cached writes through
  3113. * shmemfs and that our cache domain tracking remains valid.
  3114. * For example, if the obj->filp was moved to swap without us
  3115. * being notified and releasing the pages, we would mistakenly
  3116. * continue to assume that the obj remained out of the CPU cached
  3117. * domain.
  3118. */
  3119. ret = i915_gem_object_get_pages(obj);
  3120. if (ret)
  3121. return ret;
  3122. i915_gem_object_flush_cpu_write_domain(obj);
  3123. /* Serialise direct access to this object with the barriers for
  3124. * coherent writes from the GPU, by effectively invalidating the
  3125. * GTT domain upon first access.
  3126. */
  3127. if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0)
  3128. mb();
  3129. old_write_domain = obj->base.write_domain;
  3130. old_read_domains = obj->base.read_domains;
  3131. /* It should now be out of any other write domains, and we can update
  3132. * the domain values for our changes.
  3133. */
  3134. BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
  3135. obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
  3136. if (write) {
  3137. obj->base.read_domains = I915_GEM_DOMAIN_GTT;
  3138. obj->base.write_domain = I915_GEM_DOMAIN_GTT;
  3139. obj->dirty = 1;
  3140. }
  3141. trace_i915_gem_object_change_domain(obj,
  3142. old_read_domains,
  3143. old_write_domain);
  3144. /* And bump the LRU for this access */
  3145. vma = i915_gem_obj_to_ggtt(obj);
  3146. if (vma && drm_mm_node_allocated(&vma->node) && !obj->active)
  3147. list_move_tail(&vma->vm_link,
  3148. &ggtt->base.inactive_list);
  3149. return 0;
  3150. }
  3151. /**
  3152. * Changes the cache-level of an object across all VMA.
  3153. *
  3154. * After this function returns, the object will be in the new cache-level
  3155. * across all GTT and the contents of the backing storage will be coherent,
  3156. * with respect to the new cache-level. In order to keep the backing storage
  3157. * coherent for all users, we only allow a single cache level to be set
  3158. * globally on the object and prevent it from being changed whilst the
  3159. * hardware is reading from the object. That is if the object is currently
  3160. * on the scanout it will be set to uncached (or equivalent display
  3161. * cache coherency) and all non-MOCS GPU access will also be uncached so
  3162. * that all direct access to the scanout remains coherent.
  3163. */
  3164. int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,
  3165. enum i915_cache_level cache_level)
  3166. {
  3167. struct drm_device *dev = obj->base.dev;
  3168. struct i915_vma *vma, *next;
  3169. bool bound = false;
  3170. int ret = 0;
  3171. if (obj->cache_level == cache_level)
  3172. goto out;
  3173. /* Inspect the list of currently bound VMA and unbind any that would
  3174. * be invalid given the new cache-level. This is principally to
  3175. * catch the issue of the CS prefetch crossing page boundaries and
  3176. * reading an invalid PTE on older architectures.
  3177. */
  3178. list_for_each_entry_safe(vma, next, &obj->vma_list, obj_link) {
  3179. if (!drm_mm_node_allocated(&vma->node))
  3180. continue;
  3181. if (vma->pin_count) {
  3182. DRM_DEBUG("can not change the cache level of pinned objects\n");
  3183. return -EBUSY;
  3184. }
  3185. if (!i915_gem_valid_gtt_space(vma, cache_level)) {
  3186. ret = i915_vma_unbind(vma);
  3187. if (ret)
  3188. return ret;
  3189. } else
  3190. bound = true;
  3191. }
  3192. /* We can reuse the existing drm_mm nodes but need to change the
  3193. * cache-level on the PTE. We could simply unbind them all and
  3194. * rebind with the correct cache-level on next use. However since
  3195. * we already have a valid slot, dma mapping, pages etc, we may as
  3196. * rewrite the PTE in the belief that doing so tramples upon less
  3197. * state and so involves less work.
  3198. */
  3199. if (bound) {
  3200. /* Before we change the PTE, the GPU must not be accessing it.
  3201. * If we wait upon the object, we know that all the bound
  3202. * VMA are no longer active.
  3203. */
  3204. ret = i915_gem_object_wait_rendering(obj, false);
  3205. if (ret)
  3206. return ret;
  3207. if (!HAS_LLC(dev) && cache_level != I915_CACHE_NONE) {
  3208. /* Access to snoopable pages through the GTT is
  3209. * incoherent and on some machines causes a hard
  3210. * lockup. Relinquish the CPU mmaping to force
  3211. * userspace to refault in the pages and we can
  3212. * then double check if the GTT mapping is still
  3213. * valid for that pointer access.
  3214. */
  3215. i915_gem_release_mmap(obj);
  3216. /* As we no longer need a fence for GTT access,
  3217. * we can relinquish it now (and so prevent having
  3218. * to steal a fence from someone else on the next
  3219. * fence request). Note GPU activity would have
  3220. * dropped the fence as all snoopable access is
  3221. * supposed to be linear.
  3222. */
  3223. ret = i915_gem_object_put_fence(obj);
  3224. if (ret)
  3225. return ret;
  3226. } else {
  3227. /* We either have incoherent backing store and
  3228. * so no GTT access or the architecture is fully
  3229. * coherent. In such cases, existing GTT mmaps
  3230. * ignore the cache bit in the PTE and we can
  3231. * rewrite it without confusing the GPU or having
  3232. * to force userspace to fault back in its mmaps.
  3233. */
  3234. }
  3235. list_for_each_entry(vma, &obj->vma_list, obj_link) {
  3236. if (!drm_mm_node_allocated(&vma->node))
  3237. continue;
  3238. ret = i915_vma_bind(vma, cache_level, PIN_UPDATE);
  3239. if (ret)
  3240. return ret;
  3241. }
  3242. }
  3243. list_for_each_entry(vma, &obj->vma_list, obj_link)
  3244. vma->node.color = cache_level;
  3245. obj->cache_level = cache_level;
  3246. out:
  3247. /* Flush the dirty CPU caches to the backing storage so that the
  3248. * object is now coherent at its new cache level (with respect
  3249. * to the access domain).
  3250. */
  3251. if (obj->cache_dirty &&
  3252. obj->base.write_domain != I915_GEM_DOMAIN_CPU &&
  3253. cpu_write_needs_clflush(obj)) {
  3254. if (i915_gem_clflush_object(obj, true))
  3255. i915_gem_chipset_flush(to_i915(obj->base.dev));
  3256. }
  3257. return 0;
  3258. }
  3259. int i915_gem_get_caching_ioctl(struct drm_device *dev, void *data,
  3260. struct drm_file *file)
  3261. {
  3262. struct drm_i915_gem_caching *args = data;
  3263. struct drm_i915_gem_object *obj;
  3264. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  3265. if (&obj->base == NULL)
  3266. return -ENOENT;
  3267. switch (obj->cache_level) {
  3268. case I915_CACHE_LLC:
  3269. case I915_CACHE_L3_LLC:
  3270. args->caching = I915_CACHING_CACHED;
  3271. break;
  3272. case I915_CACHE_WT:
  3273. args->caching = I915_CACHING_DISPLAY;
  3274. break;
  3275. default:
  3276. args->caching = I915_CACHING_NONE;
  3277. break;
  3278. }
  3279. drm_gem_object_unreference_unlocked(&obj->base);
  3280. return 0;
  3281. }
  3282. int i915_gem_set_caching_ioctl(struct drm_device *dev, void *data,
  3283. struct drm_file *file)
  3284. {
  3285. struct drm_i915_private *dev_priv = dev->dev_private;
  3286. struct drm_i915_gem_caching *args = data;
  3287. struct drm_i915_gem_object *obj;
  3288. enum i915_cache_level level;
  3289. int ret;
  3290. switch (args->caching) {
  3291. case I915_CACHING_NONE:
  3292. level = I915_CACHE_NONE;
  3293. break;
  3294. case I915_CACHING_CACHED:
  3295. /*
  3296. * Due to a HW issue on BXT A stepping, GPU stores via a
  3297. * snooped mapping may leave stale data in a corresponding CPU
  3298. * cacheline, whereas normally such cachelines would get
  3299. * invalidated.
  3300. */
  3301. if (!HAS_LLC(dev) && !HAS_SNOOP(dev))
  3302. return -ENODEV;
  3303. level = I915_CACHE_LLC;
  3304. break;
  3305. case I915_CACHING_DISPLAY:
  3306. level = HAS_WT(dev) ? I915_CACHE_WT : I915_CACHE_NONE;
  3307. break;
  3308. default:
  3309. return -EINVAL;
  3310. }
  3311. intel_runtime_pm_get(dev_priv);
  3312. ret = i915_mutex_lock_interruptible(dev);
  3313. if (ret)
  3314. goto rpm_put;
  3315. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  3316. if (&obj->base == NULL) {
  3317. ret = -ENOENT;
  3318. goto unlock;
  3319. }
  3320. ret = i915_gem_object_set_cache_level(obj, level);
  3321. drm_gem_object_unreference(&obj->base);
  3322. unlock:
  3323. mutex_unlock(&dev->struct_mutex);
  3324. rpm_put:
  3325. intel_runtime_pm_put(dev_priv);
  3326. return ret;
  3327. }
  3328. /*
  3329. * Prepare buffer for display plane (scanout, cursors, etc).
  3330. * Can be called from an uninterruptible phase (modesetting) and allows
  3331. * any flushes to be pipelined (for pageflips).
  3332. */
  3333. int
  3334. i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj,
  3335. u32 alignment,
  3336. const struct i915_ggtt_view *view)
  3337. {
  3338. u32 old_read_domains, old_write_domain;
  3339. int ret;
  3340. /* Mark the pin_display early so that we account for the
  3341. * display coherency whilst setting up the cache domains.
  3342. */
  3343. obj->pin_display++;
  3344. /* The display engine is not coherent with the LLC cache on gen6. As
  3345. * a result, we make sure that the pinning that is about to occur is
  3346. * done with uncached PTEs. This is lowest common denominator for all
  3347. * chipsets.
  3348. *
  3349. * However for gen6+, we could do better by using the GFDT bit instead
  3350. * of uncaching, which would allow us to flush all the LLC-cached data
  3351. * with that bit in the PTE to main memory with just one PIPE_CONTROL.
  3352. */
  3353. ret = i915_gem_object_set_cache_level(obj,
  3354. HAS_WT(obj->base.dev) ? I915_CACHE_WT : I915_CACHE_NONE);
  3355. if (ret)
  3356. goto err_unpin_display;
  3357. /* As the user may map the buffer once pinned in the display plane
  3358. * (e.g. libkms for the bootup splash), we have to ensure that we
  3359. * always use map_and_fenceable for all scanout buffers.
  3360. */
  3361. ret = i915_gem_object_ggtt_pin(obj, view, alignment,
  3362. view->type == I915_GGTT_VIEW_NORMAL ?
  3363. PIN_MAPPABLE : 0);
  3364. if (ret)
  3365. goto err_unpin_display;
  3366. i915_gem_object_flush_cpu_write_domain(obj);
  3367. old_write_domain = obj->base.write_domain;
  3368. old_read_domains = obj->base.read_domains;
  3369. /* It should now be out of any other write domains, and we can update
  3370. * the domain values for our changes.
  3371. */
  3372. obj->base.write_domain = 0;
  3373. obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
  3374. trace_i915_gem_object_change_domain(obj,
  3375. old_read_domains,
  3376. old_write_domain);
  3377. return 0;
  3378. err_unpin_display:
  3379. obj->pin_display--;
  3380. return ret;
  3381. }
  3382. void
  3383. i915_gem_object_unpin_from_display_plane(struct drm_i915_gem_object *obj,
  3384. const struct i915_ggtt_view *view)
  3385. {
  3386. if (WARN_ON(obj->pin_display == 0))
  3387. return;
  3388. i915_gem_object_ggtt_unpin_view(obj, view);
  3389. obj->pin_display--;
  3390. }
  3391. /**
  3392. * Moves a single object to the CPU read, and possibly write domain.
  3393. *
  3394. * This function returns when the move is complete, including waiting on
  3395. * flushes to occur.
  3396. */
  3397. int
  3398. i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write)
  3399. {
  3400. uint32_t old_write_domain, old_read_domains;
  3401. int ret;
  3402. if (obj->base.write_domain == I915_GEM_DOMAIN_CPU)
  3403. return 0;
  3404. ret = i915_gem_object_wait_rendering(obj, !write);
  3405. if (ret)
  3406. return ret;
  3407. i915_gem_object_flush_gtt_write_domain(obj);
  3408. old_write_domain = obj->base.write_domain;
  3409. old_read_domains = obj->base.read_domains;
  3410. /* Flush the CPU cache if it's still invalid. */
  3411. if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0) {
  3412. i915_gem_clflush_object(obj, false);
  3413. obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
  3414. }
  3415. /* It should now be out of any other write domains, and we can update
  3416. * the domain values for our changes.
  3417. */
  3418. BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
  3419. /* If we're writing through the CPU, then the GPU read domains will
  3420. * need to be invalidated at next use.
  3421. */
  3422. if (write) {
  3423. obj->base.read_domains = I915_GEM_DOMAIN_CPU;
  3424. obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  3425. }
  3426. trace_i915_gem_object_change_domain(obj,
  3427. old_read_domains,
  3428. old_write_domain);
  3429. return 0;
  3430. }
  3431. /* Throttle our rendering by waiting until the ring has completed our requests
  3432. * emitted over 20 msec ago.
  3433. *
  3434. * Note that if we were to use the current jiffies each time around the loop,
  3435. * we wouldn't escape the function with any frames outstanding if the time to
  3436. * render a frame was over 20ms.
  3437. *
  3438. * This should get us reasonable parallelism between CPU and GPU but also
  3439. * relatively low latency when blocking on a particular request to finish.
  3440. */
  3441. static int
  3442. i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
  3443. {
  3444. struct drm_i915_private *dev_priv = dev->dev_private;
  3445. struct drm_i915_file_private *file_priv = file->driver_priv;
  3446. unsigned long recent_enough = jiffies - DRM_I915_THROTTLE_JIFFIES;
  3447. struct drm_i915_gem_request *request, *target = NULL;
  3448. int ret;
  3449. ret = i915_gem_wait_for_error(&dev_priv->gpu_error);
  3450. if (ret)
  3451. return ret;
  3452. /* ABI: return -EIO if already wedged */
  3453. if (i915_terminally_wedged(&dev_priv->gpu_error))
  3454. return -EIO;
  3455. spin_lock(&file_priv->mm.lock);
  3456. list_for_each_entry(request, &file_priv->mm.request_list, client_list) {
  3457. if (time_after_eq(request->emitted_jiffies, recent_enough))
  3458. break;
  3459. /*
  3460. * Note that the request might not have been submitted yet.
  3461. * In which case emitted_jiffies will be zero.
  3462. */
  3463. if (!request->emitted_jiffies)
  3464. continue;
  3465. target = request;
  3466. }
  3467. if (target)
  3468. i915_gem_request_reference(target);
  3469. spin_unlock(&file_priv->mm.lock);
  3470. if (target == NULL)
  3471. return 0;
  3472. ret = __i915_wait_request(target, true, NULL, NULL);
  3473. if (ret == 0)
  3474. queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, 0);
  3475. i915_gem_request_unreference(target);
  3476. return ret;
  3477. }
  3478. static bool
  3479. i915_vma_misplaced(struct i915_vma *vma, uint32_t alignment, uint64_t flags)
  3480. {
  3481. struct drm_i915_gem_object *obj = vma->obj;
  3482. if (alignment &&
  3483. vma->node.start & (alignment - 1))
  3484. return true;
  3485. if (flags & PIN_MAPPABLE && !obj->map_and_fenceable)
  3486. return true;
  3487. if (flags & PIN_OFFSET_BIAS &&
  3488. vma->node.start < (flags & PIN_OFFSET_MASK))
  3489. return true;
  3490. if (flags & PIN_OFFSET_FIXED &&
  3491. vma->node.start != (flags & PIN_OFFSET_MASK))
  3492. return true;
  3493. return false;
  3494. }
  3495. void __i915_vma_set_map_and_fenceable(struct i915_vma *vma)
  3496. {
  3497. struct drm_i915_gem_object *obj = vma->obj;
  3498. bool mappable, fenceable;
  3499. u32 fence_size, fence_alignment;
  3500. fence_size = i915_gem_get_gtt_size(obj->base.dev,
  3501. obj->base.size,
  3502. obj->tiling_mode);
  3503. fence_alignment = i915_gem_get_gtt_alignment(obj->base.dev,
  3504. obj->base.size,
  3505. obj->tiling_mode,
  3506. true);
  3507. fenceable = (vma->node.size == fence_size &&
  3508. (vma->node.start & (fence_alignment - 1)) == 0);
  3509. mappable = (vma->node.start + fence_size <=
  3510. to_i915(obj->base.dev)->ggtt.mappable_end);
  3511. obj->map_and_fenceable = mappable && fenceable;
  3512. }
  3513. static int
  3514. i915_gem_object_do_pin(struct drm_i915_gem_object *obj,
  3515. struct i915_address_space *vm,
  3516. const struct i915_ggtt_view *ggtt_view,
  3517. uint32_t alignment,
  3518. uint64_t flags)
  3519. {
  3520. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  3521. struct i915_vma *vma;
  3522. unsigned bound;
  3523. int ret;
  3524. if (WARN_ON(vm == &dev_priv->mm.aliasing_ppgtt->base))
  3525. return -ENODEV;
  3526. if (WARN_ON(flags & (PIN_GLOBAL | PIN_MAPPABLE) && !i915_is_ggtt(vm)))
  3527. return -EINVAL;
  3528. if (WARN_ON((flags & (PIN_MAPPABLE | PIN_GLOBAL)) == PIN_MAPPABLE))
  3529. return -EINVAL;
  3530. if (WARN_ON(i915_is_ggtt(vm) != !!ggtt_view))
  3531. return -EINVAL;
  3532. vma = ggtt_view ? i915_gem_obj_to_ggtt_view(obj, ggtt_view) :
  3533. i915_gem_obj_to_vma(obj, vm);
  3534. if (vma) {
  3535. if (WARN_ON(vma->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT))
  3536. return -EBUSY;
  3537. if (i915_vma_misplaced(vma, alignment, flags)) {
  3538. WARN(vma->pin_count,
  3539. "bo is already pinned in %s with incorrect alignment:"
  3540. " offset=%08x %08x, req.alignment=%x, req.map_and_fenceable=%d,"
  3541. " obj->map_and_fenceable=%d\n",
  3542. ggtt_view ? "ggtt" : "ppgtt",
  3543. upper_32_bits(vma->node.start),
  3544. lower_32_bits(vma->node.start),
  3545. alignment,
  3546. !!(flags & PIN_MAPPABLE),
  3547. obj->map_and_fenceable);
  3548. ret = i915_vma_unbind(vma);
  3549. if (ret)
  3550. return ret;
  3551. vma = NULL;
  3552. }
  3553. }
  3554. bound = vma ? vma->bound : 0;
  3555. if (vma == NULL || !drm_mm_node_allocated(&vma->node)) {
  3556. vma = i915_gem_object_bind_to_vm(obj, vm, ggtt_view, alignment,
  3557. flags);
  3558. if (IS_ERR(vma))
  3559. return PTR_ERR(vma);
  3560. } else {
  3561. ret = i915_vma_bind(vma, obj->cache_level, flags);
  3562. if (ret)
  3563. return ret;
  3564. }
  3565. if (ggtt_view && ggtt_view->type == I915_GGTT_VIEW_NORMAL &&
  3566. (bound ^ vma->bound) & GLOBAL_BIND) {
  3567. __i915_vma_set_map_and_fenceable(vma);
  3568. WARN_ON(flags & PIN_MAPPABLE && !obj->map_and_fenceable);
  3569. }
  3570. vma->pin_count++;
  3571. return 0;
  3572. }
  3573. int
  3574. i915_gem_object_pin(struct drm_i915_gem_object *obj,
  3575. struct i915_address_space *vm,
  3576. uint32_t alignment,
  3577. uint64_t flags)
  3578. {
  3579. return i915_gem_object_do_pin(obj, vm,
  3580. i915_is_ggtt(vm) ? &i915_ggtt_view_normal : NULL,
  3581. alignment, flags);
  3582. }
  3583. int
  3584. i915_gem_object_ggtt_pin(struct drm_i915_gem_object *obj,
  3585. const struct i915_ggtt_view *view,
  3586. uint32_t alignment,
  3587. uint64_t flags)
  3588. {
  3589. struct drm_device *dev = obj->base.dev;
  3590. struct drm_i915_private *dev_priv = to_i915(dev);
  3591. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  3592. BUG_ON(!view);
  3593. return i915_gem_object_do_pin(obj, &ggtt->base, view,
  3594. alignment, flags | PIN_GLOBAL);
  3595. }
  3596. void
  3597. i915_gem_object_ggtt_unpin_view(struct drm_i915_gem_object *obj,
  3598. const struct i915_ggtt_view *view)
  3599. {
  3600. struct i915_vma *vma = i915_gem_obj_to_ggtt_view(obj, view);
  3601. WARN_ON(vma->pin_count == 0);
  3602. WARN_ON(!i915_gem_obj_ggtt_bound_view(obj, view));
  3603. --vma->pin_count;
  3604. }
  3605. int
  3606. i915_gem_busy_ioctl(struct drm_device *dev, void *data,
  3607. struct drm_file *file)
  3608. {
  3609. struct drm_i915_gem_busy *args = data;
  3610. struct drm_i915_gem_object *obj;
  3611. int ret;
  3612. ret = i915_mutex_lock_interruptible(dev);
  3613. if (ret)
  3614. return ret;
  3615. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  3616. if (&obj->base == NULL) {
  3617. ret = -ENOENT;
  3618. goto unlock;
  3619. }
  3620. /* Count all active objects as busy, even if they are currently not used
  3621. * by the gpu. Users of this interface expect objects to eventually
  3622. * become non-busy without any further actions, therefore emit any
  3623. * necessary flushes here.
  3624. */
  3625. ret = i915_gem_object_flush_active(obj);
  3626. if (ret)
  3627. goto unref;
  3628. args->busy = 0;
  3629. if (obj->active) {
  3630. int i;
  3631. for (i = 0; i < I915_NUM_ENGINES; i++) {
  3632. struct drm_i915_gem_request *req;
  3633. req = obj->last_read_req[i];
  3634. if (req)
  3635. args->busy |= 1 << (16 + req->engine->exec_id);
  3636. }
  3637. if (obj->last_write_req)
  3638. args->busy |= obj->last_write_req->engine->exec_id;
  3639. }
  3640. unref:
  3641. drm_gem_object_unreference(&obj->base);
  3642. unlock:
  3643. mutex_unlock(&dev->struct_mutex);
  3644. return ret;
  3645. }
  3646. int
  3647. i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
  3648. struct drm_file *file_priv)
  3649. {
  3650. return i915_gem_ring_throttle(dev, file_priv);
  3651. }
  3652. int
  3653. i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
  3654. struct drm_file *file_priv)
  3655. {
  3656. struct drm_i915_private *dev_priv = dev->dev_private;
  3657. struct drm_i915_gem_madvise *args = data;
  3658. struct drm_i915_gem_object *obj;
  3659. int ret;
  3660. switch (args->madv) {
  3661. case I915_MADV_DONTNEED:
  3662. case I915_MADV_WILLNEED:
  3663. break;
  3664. default:
  3665. return -EINVAL;
  3666. }
  3667. ret = i915_mutex_lock_interruptible(dev);
  3668. if (ret)
  3669. return ret;
  3670. obj = to_intel_bo(drm_gem_object_lookup(dev, file_priv, args->handle));
  3671. if (&obj->base == NULL) {
  3672. ret = -ENOENT;
  3673. goto unlock;
  3674. }
  3675. if (i915_gem_obj_is_pinned(obj)) {
  3676. ret = -EINVAL;
  3677. goto out;
  3678. }
  3679. if (obj->pages &&
  3680. obj->tiling_mode != I915_TILING_NONE &&
  3681. dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
  3682. if (obj->madv == I915_MADV_WILLNEED)
  3683. i915_gem_object_unpin_pages(obj);
  3684. if (args->madv == I915_MADV_WILLNEED)
  3685. i915_gem_object_pin_pages(obj);
  3686. }
  3687. if (obj->madv != __I915_MADV_PURGED)
  3688. obj->madv = args->madv;
  3689. /* if the object is no longer attached, discard its backing storage */
  3690. if (obj->madv == I915_MADV_DONTNEED && obj->pages == NULL)
  3691. i915_gem_object_truncate(obj);
  3692. args->retained = obj->madv != __I915_MADV_PURGED;
  3693. out:
  3694. drm_gem_object_unreference(&obj->base);
  3695. unlock:
  3696. mutex_unlock(&dev->struct_mutex);
  3697. return ret;
  3698. }
  3699. void i915_gem_object_init(struct drm_i915_gem_object *obj,
  3700. const struct drm_i915_gem_object_ops *ops)
  3701. {
  3702. int i;
  3703. INIT_LIST_HEAD(&obj->global_list);
  3704. for (i = 0; i < I915_NUM_ENGINES; i++)
  3705. INIT_LIST_HEAD(&obj->engine_list[i]);
  3706. INIT_LIST_HEAD(&obj->obj_exec_link);
  3707. INIT_LIST_HEAD(&obj->vma_list);
  3708. INIT_LIST_HEAD(&obj->batch_pool_link);
  3709. obj->ops = ops;
  3710. obj->fence_reg = I915_FENCE_REG_NONE;
  3711. obj->madv = I915_MADV_WILLNEED;
  3712. i915_gem_info_add_obj(obj->base.dev->dev_private, obj->base.size);
  3713. }
  3714. static const struct drm_i915_gem_object_ops i915_gem_object_ops = {
  3715. .flags = I915_GEM_OBJECT_HAS_STRUCT_PAGE,
  3716. .get_pages = i915_gem_object_get_pages_gtt,
  3717. .put_pages = i915_gem_object_put_pages_gtt,
  3718. };
  3719. struct drm_i915_gem_object *i915_gem_object_create(struct drm_device *dev,
  3720. size_t size)
  3721. {
  3722. struct drm_i915_gem_object *obj;
  3723. struct address_space *mapping;
  3724. gfp_t mask;
  3725. int ret;
  3726. obj = i915_gem_object_alloc(dev);
  3727. if (obj == NULL)
  3728. return ERR_PTR(-ENOMEM);
  3729. ret = drm_gem_object_init(dev, &obj->base, size);
  3730. if (ret)
  3731. goto fail;
  3732. mask = GFP_HIGHUSER | __GFP_RECLAIMABLE;
  3733. if (IS_CRESTLINE(dev) || IS_BROADWATER(dev)) {
  3734. /* 965gm cannot relocate objects above 4GiB. */
  3735. mask &= ~__GFP_HIGHMEM;
  3736. mask |= __GFP_DMA32;
  3737. }
  3738. mapping = file_inode(obj->base.filp)->i_mapping;
  3739. mapping_set_gfp_mask(mapping, mask);
  3740. i915_gem_object_init(obj, &i915_gem_object_ops);
  3741. obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  3742. obj->base.read_domains = I915_GEM_DOMAIN_CPU;
  3743. if (HAS_LLC(dev)) {
  3744. /* On some devices, we can have the GPU use the LLC (the CPU
  3745. * cache) for about a 10% performance improvement
  3746. * compared to uncached. Graphics requests other than
  3747. * display scanout are coherent with the CPU in
  3748. * accessing this cache. This means in this mode we
  3749. * don't need to clflush on the CPU side, and on the
  3750. * GPU side we only need to flush internal caches to
  3751. * get data visible to the CPU.
  3752. *
  3753. * However, we maintain the display planes as UC, and so
  3754. * need to rebind when first used as such.
  3755. */
  3756. obj->cache_level = I915_CACHE_LLC;
  3757. } else
  3758. obj->cache_level = I915_CACHE_NONE;
  3759. trace_i915_gem_object_create(obj);
  3760. return obj;
  3761. fail:
  3762. i915_gem_object_free(obj);
  3763. return ERR_PTR(ret);
  3764. }
  3765. static bool discard_backing_storage(struct drm_i915_gem_object *obj)
  3766. {
  3767. /* If we are the last user of the backing storage (be it shmemfs
  3768. * pages or stolen etc), we know that the pages are going to be
  3769. * immediately released. In this case, we can then skip copying
  3770. * back the contents from the GPU.
  3771. */
  3772. if (obj->madv != I915_MADV_WILLNEED)
  3773. return false;
  3774. if (obj->base.filp == NULL)
  3775. return true;
  3776. /* At first glance, this looks racy, but then again so would be
  3777. * userspace racing mmap against close. However, the first external
  3778. * reference to the filp can only be obtained through the
  3779. * i915_gem_mmap_ioctl() which safeguards us against the user
  3780. * acquiring such a reference whilst we are in the middle of
  3781. * freeing the object.
  3782. */
  3783. return atomic_long_read(&obj->base.filp->f_count) == 1;
  3784. }
  3785. void i915_gem_free_object(struct drm_gem_object *gem_obj)
  3786. {
  3787. struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
  3788. struct drm_device *dev = obj->base.dev;
  3789. struct drm_i915_private *dev_priv = dev->dev_private;
  3790. struct i915_vma *vma, *next;
  3791. intel_runtime_pm_get(dev_priv);
  3792. trace_i915_gem_object_destroy(obj);
  3793. list_for_each_entry_safe(vma, next, &obj->vma_list, obj_link) {
  3794. int ret;
  3795. vma->pin_count = 0;
  3796. ret = i915_vma_unbind(vma);
  3797. if (WARN_ON(ret == -ERESTARTSYS)) {
  3798. bool was_interruptible;
  3799. was_interruptible = dev_priv->mm.interruptible;
  3800. dev_priv->mm.interruptible = false;
  3801. WARN_ON(i915_vma_unbind(vma));
  3802. dev_priv->mm.interruptible = was_interruptible;
  3803. }
  3804. }
  3805. /* Stolen objects don't hold a ref, but do hold pin count. Fix that up
  3806. * before progressing. */
  3807. if (obj->stolen)
  3808. i915_gem_object_unpin_pages(obj);
  3809. WARN_ON(obj->frontbuffer_bits);
  3810. if (obj->pages && obj->madv == I915_MADV_WILLNEED &&
  3811. dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES &&
  3812. obj->tiling_mode != I915_TILING_NONE)
  3813. i915_gem_object_unpin_pages(obj);
  3814. if (WARN_ON(obj->pages_pin_count))
  3815. obj->pages_pin_count = 0;
  3816. if (discard_backing_storage(obj))
  3817. obj->madv = I915_MADV_DONTNEED;
  3818. i915_gem_object_put_pages(obj);
  3819. i915_gem_object_free_mmap_offset(obj);
  3820. BUG_ON(obj->pages);
  3821. if (obj->base.import_attach)
  3822. drm_prime_gem_destroy(&obj->base, NULL);
  3823. if (obj->ops->release)
  3824. obj->ops->release(obj);
  3825. drm_gem_object_release(&obj->base);
  3826. i915_gem_info_remove_obj(dev_priv, obj->base.size);
  3827. kfree(obj->bit_17);
  3828. i915_gem_object_free(obj);
  3829. intel_runtime_pm_put(dev_priv);
  3830. }
  3831. struct i915_vma *i915_gem_obj_to_vma(struct drm_i915_gem_object *obj,
  3832. struct i915_address_space *vm)
  3833. {
  3834. struct i915_vma *vma;
  3835. list_for_each_entry(vma, &obj->vma_list, obj_link) {
  3836. if (vma->ggtt_view.type == I915_GGTT_VIEW_NORMAL &&
  3837. vma->vm == vm)
  3838. return vma;
  3839. }
  3840. return NULL;
  3841. }
  3842. struct i915_vma *i915_gem_obj_to_ggtt_view(struct drm_i915_gem_object *obj,
  3843. const struct i915_ggtt_view *view)
  3844. {
  3845. struct i915_vma *vma;
  3846. GEM_BUG_ON(!view);
  3847. list_for_each_entry(vma, &obj->vma_list, obj_link)
  3848. if (vma->is_ggtt && i915_ggtt_view_equal(&vma->ggtt_view, view))
  3849. return vma;
  3850. return NULL;
  3851. }
  3852. void i915_gem_vma_destroy(struct i915_vma *vma)
  3853. {
  3854. WARN_ON(vma->node.allocated);
  3855. /* Keep the vma as a placeholder in the execbuffer reservation lists */
  3856. if (!list_empty(&vma->exec_list))
  3857. return;
  3858. if (!vma->is_ggtt)
  3859. i915_ppgtt_put(i915_vm_to_ppgtt(vma->vm));
  3860. list_del(&vma->obj_link);
  3861. kmem_cache_free(to_i915(vma->obj->base.dev)->vmas, vma);
  3862. }
  3863. static void
  3864. i915_gem_stop_engines(struct drm_device *dev)
  3865. {
  3866. struct drm_i915_private *dev_priv = dev->dev_private;
  3867. struct intel_engine_cs *engine;
  3868. for_each_engine(engine, dev_priv)
  3869. dev_priv->gt.stop_engine(engine);
  3870. }
  3871. int
  3872. i915_gem_suspend(struct drm_device *dev)
  3873. {
  3874. struct drm_i915_private *dev_priv = dev->dev_private;
  3875. int ret = 0;
  3876. mutex_lock(&dev->struct_mutex);
  3877. ret = i915_gpu_idle(dev);
  3878. if (ret)
  3879. goto err;
  3880. i915_gem_retire_requests(dev_priv);
  3881. i915_gem_stop_engines(dev);
  3882. i915_gem_context_lost(dev_priv);
  3883. mutex_unlock(&dev->struct_mutex);
  3884. cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work);
  3885. cancel_delayed_work_sync(&dev_priv->mm.retire_work);
  3886. flush_delayed_work(&dev_priv->mm.idle_work);
  3887. /* Assert that we sucessfully flushed all the work and
  3888. * reset the GPU back to its idle, low power state.
  3889. */
  3890. WARN_ON(dev_priv->mm.busy);
  3891. return 0;
  3892. err:
  3893. mutex_unlock(&dev->struct_mutex);
  3894. return ret;
  3895. }
  3896. void i915_gem_init_swizzling(struct drm_device *dev)
  3897. {
  3898. struct drm_i915_private *dev_priv = dev->dev_private;
  3899. if (INTEL_INFO(dev)->gen < 5 ||
  3900. dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE)
  3901. return;
  3902. I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
  3903. DISP_TILE_SURFACE_SWIZZLING);
  3904. if (IS_GEN5(dev))
  3905. return;
  3906. I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL);
  3907. if (IS_GEN6(dev))
  3908. I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_SNB));
  3909. else if (IS_GEN7(dev))
  3910. I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_IVB));
  3911. else if (IS_GEN8(dev))
  3912. I915_WRITE(GAMTARBMODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_BDW));
  3913. else
  3914. BUG();
  3915. }
  3916. static void init_unused_ring(struct drm_device *dev, u32 base)
  3917. {
  3918. struct drm_i915_private *dev_priv = dev->dev_private;
  3919. I915_WRITE(RING_CTL(base), 0);
  3920. I915_WRITE(RING_HEAD(base), 0);
  3921. I915_WRITE(RING_TAIL(base), 0);
  3922. I915_WRITE(RING_START(base), 0);
  3923. }
  3924. static void init_unused_rings(struct drm_device *dev)
  3925. {
  3926. if (IS_I830(dev)) {
  3927. init_unused_ring(dev, PRB1_BASE);
  3928. init_unused_ring(dev, SRB0_BASE);
  3929. init_unused_ring(dev, SRB1_BASE);
  3930. init_unused_ring(dev, SRB2_BASE);
  3931. init_unused_ring(dev, SRB3_BASE);
  3932. } else if (IS_GEN2(dev)) {
  3933. init_unused_ring(dev, SRB0_BASE);
  3934. init_unused_ring(dev, SRB1_BASE);
  3935. } else if (IS_GEN3(dev)) {
  3936. init_unused_ring(dev, PRB1_BASE);
  3937. init_unused_ring(dev, PRB2_BASE);
  3938. }
  3939. }
  3940. int i915_gem_init_engines(struct drm_device *dev)
  3941. {
  3942. struct drm_i915_private *dev_priv = dev->dev_private;
  3943. int ret;
  3944. ret = intel_init_render_ring_buffer(dev);
  3945. if (ret)
  3946. return ret;
  3947. if (HAS_BSD(dev)) {
  3948. ret = intel_init_bsd_ring_buffer(dev);
  3949. if (ret)
  3950. goto cleanup_render_ring;
  3951. }
  3952. if (HAS_BLT(dev)) {
  3953. ret = intel_init_blt_ring_buffer(dev);
  3954. if (ret)
  3955. goto cleanup_bsd_ring;
  3956. }
  3957. if (HAS_VEBOX(dev)) {
  3958. ret = intel_init_vebox_ring_buffer(dev);
  3959. if (ret)
  3960. goto cleanup_blt_ring;
  3961. }
  3962. if (HAS_BSD2(dev)) {
  3963. ret = intel_init_bsd2_ring_buffer(dev);
  3964. if (ret)
  3965. goto cleanup_vebox_ring;
  3966. }
  3967. return 0;
  3968. cleanup_vebox_ring:
  3969. intel_cleanup_engine(&dev_priv->engine[VECS]);
  3970. cleanup_blt_ring:
  3971. intel_cleanup_engine(&dev_priv->engine[BCS]);
  3972. cleanup_bsd_ring:
  3973. intel_cleanup_engine(&dev_priv->engine[VCS]);
  3974. cleanup_render_ring:
  3975. intel_cleanup_engine(&dev_priv->engine[RCS]);
  3976. return ret;
  3977. }
  3978. int
  3979. i915_gem_init_hw(struct drm_device *dev)
  3980. {
  3981. struct drm_i915_private *dev_priv = dev->dev_private;
  3982. struct intel_engine_cs *engine;
  3983. int ret;
  3984. /* Double layer security blanket, see i915_gem_init() */
  3985. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  3986. if (HAS_EDRAM(dev) && INTEL_GEN(dev_priv) < 9)
  3987. I915_WRITE(HSW_IDICR, I915_READ(HSW_IDICR) | IDIHASHMSK(0xf));
  3988. if (IS_HASWELL(dev))
  3989. I915_WRITE(MI_PREDICATE_RESULT_2, IS_HSW_GT3(dev) ?
  3990. LOWER_SLICE_ENABLED : LOWER_SLICE_DISABLED);
  3991. if (HAS_PCH_NOP(dev)) {
  3992. if (IS_IVYBRIDGE(dev)) {
  3993. u32 temp = I915_READ(GEN7_MSG_CTL);
  3994. temp &= ~(WAIT_FOR_PCH_FLR_ACK | WAIT_FOR_PCH_RESET_ACK);
  3995. I915_WRITE(GEN7_MSG_CTL, temp);
  3996. } else if (INTEL_INFO(dev)->gen >= 7) {
  3997. u32 temp = I915_READ(HSW_NDE_RSTWRN_OPT);
  3998. temp &= ~RESET_PCH_HANDSHAKE_ENABLE;
  3999. I915_WRITE(HSW_NDE_RSTWRN_OPT, temp);
  4000. }
  4001. }
  4002. i915_gem_init_swizzling(dev);
  4003. /*
  4004. * At least 830 can leave some of the unused rings
  4005. * "active" (ie. head != tail) after resume which
  4006. * will prevent c3 entry. Makes sure all unused rings
  4007. * are totally idle.
  4008. */
  4009. init_unused_rings(dev);
  4010. BUG_ON(!dev_priv->kernel_context);
  4011. ret = i915_ppgtt_init_hw(dev);
  4012. if (ret) {
  4013. DRM_ERROR("PPGTT enable HW failed %d\n", ret);
  4014. goto out;
  4015. }
  4016. /* Need to do basic initialisation of all rings first: */
  4017. for_each_engine(engine, dev_priv) {
  4018. ret = engine->init_hw(engine);
  4019. if (ret)
  4020. goto out;
  4021. }
  4022. intel_mocs_init_l3cc_table(dev);
  4023. /* We can't enable contexts until all firmware is loaded */
  4024. if (HAS_GUC_UCODE(dev)) {
  4025. ret = intel_guc_ucode_load(dev);
  4026. if (ret) {
  4027. DRM_ERROR("Failed to initialize GuC, error %d\n", ret);
  4028. ret = -EIO;
  4029. goto out;
  4030. }
  4031. }
  4032. /*
  4033. * Increment the next seqno by 0x100 so we have a visible break
  4034. * on re-initialisation
  4035. */
  4036. ret = i915_gem_set_seqno(dev, dev_priv->next_seqno+0x100);
  4037. out:
  4038. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  4039. return ret;
  4040. }
  4041. int i915_gem_init(struct drm_device *dev)
  4042. {
  4043. struct drm_i915_private *dev_priv = dev->dev_private;
  4044. int ret;
  4045. mutex_lock(&dev->struct_mutex);
  4046. if (!i915.enable_execlists) {
  4047. dev_priv->gt.execbuf_submit = i915_gem_ringbuffer_submission;
  4048. dev_priv->gt.init_engines = i915_gem_init_engines;
  4049. dev_priv->gt.cleanup_engine = intel_cleanup_engine;
  4050. dev_priv->gt.stop_engine = intel_stop_engine;
  4051. } else {
  4052. dev_priv->gt.execbuf_submit = intel_execlists_submission;
  4053. dev_priv->gt.init_engines = intel_logical_rings_init;
  4054. dev_priv->gt.cleanup_engine = intel_logical_ring_cleanup;
  4055. dev_priv->gt.stop_engine = intel_logical_ring_stop;
  4056. }
  4057. /* This is just a security blanket to placate dragons.
  4058. * On some systems, we very sporadically observe that the first TLBs
  4059. * used by the CS may be stale, despite us poking the TLB reset. If
  4060. * we hold the forcewake during initialisation these problems
  4061. * just magically go away.
  4062. */
  4063. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  4064. i915_gem_init_userptr(dev_priv);
  4065. i915_gem_init_ggtt(dev);
  4066. ret = i915_gem_context_init(dev);
  4067. if (ret)
  4068. goto out_unlock;
  4069. ret = dev_priv->gt.init_engines(dev);
  4070. if (ret)
  4071. goto out_unlock;
  4072. ret = i915_gem_init_hw(dev);
  4073. if (ret == -EIO) {
  4074. /* Allow ring initialisation to fail by marking the GPU as
  4075. * wedged. But we only want to do this where the GPU is angry,
  4076. * for all other failure, such as an allocation failure, bail.
  4077. */
  4078. DRM_ERROR("Failed to initialize GPU, declaring it wedged\n");
  4079. atomic_or(I915_WEDGED, &dev_priv->gpu_error.reset_counter);
  4080. ret = 0;
  4081. }
  4082. out_unlock:
  4083. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  4084. mutex_unlock(&dev->struct_mutex);
  4085. return ret;
  4086. }
  4087. void
  4088. i915_gem_cleanup_engines(struct drm_device *dev)
  4089. {
  4090. struct drm_i915_private *dev_priv = dev->dev_private;
  4091. struct intel_engine_cs *engine;
  4092. for_each_engine(engine, dev_priv)
  4093. dev_priv->gt.cleanup_engine(engine);
  4094. }
  4095. static void
  4096. init_engine_lists(struct intel_engine_cs *engine)
  4097. {
  4098. INIT_LIST_HEAD(&engine->active_list);
  4099. INIT_LIST_HEAD(&engine->request_list);
  4100. }
  4101. void
  4102. i915_gem_load_init_fences(struct drm_i915_private *dev_priv)
  4103. {
  4104. struct drm_device *dev = dev_priv->dev;
  4105. if (INTEL_INFO(dev_priv)->gen >= 7 && !IS_VALLEYVIEW(dev_priv) &&
  4106. !IS_CHERRYVIEW(dev_priv))
  4107. dev_priv->num_fence_regs = 32;
  4108. else if (INTEL_INFO(dev_priv)->gen >= 4 || IS_I945G(dev_priv) ||
  4109. IS_I945GM(dev_priv) || IS_G33(dev_priv))
  4110. dev_priv->num_fence_regs = 16;
  4111. else
  4112. dev_priv->num_fence_regs = 8;
  4113. if (intel_vgpu_active(dev_priv))
  4114. dev_priv->num_fence_regs =
  4115. I915_READ(vgtif_reg(avail_rs.fence_num));
  4116. /* Initialize fence registers to zero */
  4117. i915_gem_restore_fences(dev);
  4118. i915_gem_detect_bit_6_swizzle(dev);
  4119. }
  4120. void
  4121. i915_gem_load_init(struct drm_device *dev)
  4122. {
  4123. struct drm_i915_private *dev_priv = dev->dev_private;
  4124. int i;
  4125. dev_priv->objects =
  4126. kmem_cache_create("i915_gem_object",
  4127. sizeof(struct drm_i915_gem_object), 0,
  4128. SLAB_HWCACHE_ALIGN,
  4129. NULL);
  4130. dev_priv->vmas =
  4131. kmem_cache_create("i915_gem_vma",
  4132. sizeof(struct i915_vma), 0,
  4133. SLAB_HWCACHE_ALIGN,
  4134. NULL);
  4135. dev_priv->requests =
  4136. kmem_cache_create("i915_gem_request",
  4137. sizeof(struct drm_i915_gem_request), 0,
  4138. SLAB_HWCACHE_ALIGN,
  4139. NULL);
  4140. INIT_LIST_HEAD(&dev_priv->vm_list);
  4141. INIT_LIST_HEAD(&dev_priv->context_list);
  4142. INIT_LIST_HEAD(&dev_priv->mm.unbound_list);
  4143. INIT_LIST_HEAD(&dev_priv->mm.bound_list);
  4144. INIT_LIST_HEAD(&dev_priv->mm.fence_list);
  4145. for (i = 0; i < I915_NUM_ENGINES; i++)
  4146. init_engine_lists(&dev_priv->engine[i]);
  4147. for (i = 0; i < I915_MAX_NUM_FENCES; i++)
  4148. INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list);
  4149. INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
  4150. i915_gem_retire_work_handler);
  4151. INIT_DELAYED_WORK(&dev_priv->mm.idle_work,
  4152. i915_gem_idle_work_handler);
  4153. init_waitqueue_head(&dev_priv->gpu_error.reset_queue);
  4154. dev_priv->relative_constants_mode = I915_EXEC_CONSTANTS_REL_GENERAL;
  4155. /*
  4156. * Set initial sequence number for requests.
  4157. * Using this number allows the wraparound to happen early,
  4158. * catching any obvious problems.
  4159. */
  4160. dev_priv->next_seqno = ((u32)~0 - 0x1100);
  4161. dev_priv->last_seqno = ((u32)~0 - 0x1101);
  4162. INIT_LIST_HEAD(&dev_priv->mm.fence_list);
  4163. init_waitqueue_head(&dev_priv->pending_flip_queue);
  4164. dev_priv->mm.interruptible = true;
  4165. mutex_init(&dev_priv->fb_tracking.lock);
  4166. }
  4167. void i915_gem_load_cleanup(struct drm_device *dev)
  4168. {
  4169. struct drm_i915_private *dev_priv = to_i915(dev);
  4170. kmem_cache_destroy(dev_priv->requests);
  4171. kmem_cache_destroy(dev_priv->vmas);
  4172. kmem_cache_destroy(dev_priv->objects);
  4173. }
  4174. int i915_gem_freeze_late(struct drm_i915_private *dev_priv)
  4175. {
  4176. struct drm_i915_gem_object *obj;
  4177. /* Called just before we write the hibernation image.
  4178. *
  4179. * We need to update the domain tracking to reflect that the CPU
  4180. * will be accessing all the pages to create and restore from the
  4181. * hibernation, and so upon restoration those pages will be in the
  4182. * CPU domain.
  4183. *
  4184. * To make sure the hibernation image contains the latest state,
  4185. * we update that state just before writing out the image.
  4186. */
  4187. list_for_each_entry(obj, &dev_priv->mm.unbound_list, global_list) {
  4188. obj->base.read_domains = I915_GEM_DOMAIN_CPU;
  4189. obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  4190. }
  4191. list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
  4192. obj->base.read_domains = I915_GEM_DOMAIN_CPU;
  4193. obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  4194. }
  4195. return 0;
  4196. }
  4197. void i915_gem_release(struct drm_device *dev, struct drm_file *file)
  4198. {
  4199. struct drm_i915_file_private *file_priv = file->driver_priv;
  4200. /* Clean up our request list when the client is going away, so that
  4201. * later retire_requests won't dereference our soon-to-be-gone
  4202. * file_priv.
  4203. */
  4204. spin_lock(&file_priv->mm.lock);
  4205. while (!list_empty(&file_priv->mm.request_list)) {
  4206. struct drm_i915_gem_request *request;
  4207. request = list_first_entry(&file_priv->mm.request_list,
  4208. struct drm_i915_gem_request,
  4209. client_list);
  4210. list_del(&request->client_list);
  4211. request->file_priv = NULL;
  4212. }
  4213. spin_unlock(&file_priv->mm.lock);
  4214. if (!list_empty(&file_priv->rps.link)) {
  4215. spin_lock(&to_i915(dev)->rps.client_lock);
  4216. list_del(&file_priv->rps.link);
  4217. spin_unlock(&to_i915(dev)->rps.client_lock);
  4218. }
  4219. }
  4220. int i915_gem_open(struct drm_device *dev, struct drm_file *file)
  4221. {
  4222. struct drm_i915_file_private *file_priv;
  4223. int ret;
  4224. DRM_DEBUG_DRIVER("\n");
  4225. file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL);
  4226. if (!file_priv)
  4227. return -ENOMEM;
  4228. file->driver_priv = file_priv;
  4229. file_priv->dev_priv = dev->dev_private;
  4230. file_priv->file = file;
  4231. INIT_LIST_HEAD(&file_priv->rps.link);
  4232. spin_lock_init(&file_priv->mm.lock);
  4233. INIT_LIST_HEAD(&file_priv->mm.request_list);
  4234. file_priv->bsd_ring = -1;
  4235. ret = i915_gem_context_open(dev, file);
  4236. if (ret)
  4237. kfree(file_priv);
  4238. return ret;
  4239. }
  4240. /**
  4241. * i915_gem_track_fb - update frontbuffer tracking
  4242. * @old: current GEM buffer for the frontbuffer slots
  4243. * @new: new GEM buffer for the frontbuffer slots
  4244. * @frontbuffer_bits: bitmask of frontbuffer slots
  4245. *
  4246. * This updates the frontbuffer tracking bits @frontbuffer_bits by clearing them
  4247. * from @old and setting them in @new. Both @old and @new can be NULL.
  4248. */
  4249. void i915_gem_track_fb(struct drm_i915_gem_object *old,
  4250. struct drm_i915_gem_object *new,
  4251. unsigned frontbuffer_bits)
  4252. {
  4253. if (old) {
  4254. WARN_ON(!mutex_is_locked(&old->base.dev->struct_mutex));
  4255. WARN_ON(!(old->frontbuffer_bits & frontbuffer_bits));
  4256. old->frontbuffer_bits &= ~frontbuffer_bits;
  4257. }
  4258. if (new) {
  4259. WARN_ON(!mutex_is_locked(&new->base.dev->struct_mutex));
  4260. WARN_ON(new->frontbuffer_bits & frontbuffer_bits);
  4261. new->frontbuffer_bits |= frontbuffer_bits;
  4262. }
  4263. }
  4264. /* All the new VM stuff */
  4265. u64 i915_gem_obj_offset(struct drm_i915_gem_object *o,
  4266. struct i915_address_space *vm)
  4267. {
  4268. struct drm_i915_private *dev_priv = o->base.dev->dev_private;
  4269. struct i915_vma *vma;
  4270. WARN_ON(vm == &dev_priv->mm.aliasing_ppgtt->base);
  4271. list_for_each_entry(vma, &o->vma_list, obj_link) {
  4272. if (vma->is_ggtt &&
  4273. vma->ggtt_view.type != I915_GGTT_VIEW_NORMAL)
  4274. continue;
  4275. if (vma->vm == vm)
  4276. return vma->node.start;
  4277. }
  4278. WARN(1, "%s vma for this object not found.\n",
  4279. i915_is_ggtt(vm) ? "global" : "ppgtt");
  4280. return -1;
  4281. }
  4282. u64 i915_gem_obj_ggtt_offset_view(struct drm_i915_gem_object *o,
  4283. const struct i915_ggtt_view *view)
  4284. {
  4285. struct i915_vma *vma;
  4286. list_for_each_entry(vma, &o->vma_list, obj_link)
  4287. if (vma->is_ggtt && i915_ggtt_view_equal(&vma->ggtt_view, view))
  4288. return vma->node.start;
  4289. WARN(1, "global vma for this object not found. (view=%u)\n", view->type);
  4290. return -1;
  4291. }
  4292. bool i915_gem_obj_bound(struct drm_i915_gem_object *o,
  4293. struct i915_address_space *vm)
  4294. {
  4295. struct i915_vma *vma;
  4296. list_for_each_entry(vma, &o->vma_list, obj_link) {
  4297. if (vma->is_ggtt &&
  4298. vma->ggtt_view.type != I915_GGTT_VIEW_NORMAL)
  4299. continue;
  4300. if (vma->vm == vm && drm_mm_node_allocated(&vma->node))
  4301. return true;
  4302. }
  4303. return false;
  4304. }
  4305. bool i915_gem_obj_ggtt_bound_view(struct drm_i915_gem_object *o,
  4306. const struct i915_ggtt_view *view)
  4307. {
  4308. struct i915_vma *vma;
  4309. list_for_each_entry(vma, &o->vma_list, obj_link)
  4310. if (vma->is_ggtt &&
  4311. i915_ggtt_view_equal(&vma->ggtt_view, view) &&
  4312. drm_mm_node_allocated(&vma->node))
  4313. return true;
  4314. return false;
  4315. }
  4316. bool i915_gem_obj_bound_any(struct drm_i915_gem_object *o)
  4317. {
  4318. struct i915_vma *vma;
  4319. list_for_each_entry(vma, &o->vma_list, obj_link)
  4320. if (drm_mm_node_allocated(&vma->node))
  4321. return true;
  4322. return false;
  4323. }
  4324. unsigned long i915_gem_obj_ggtt_size(struct drm_i915_gem_object *o)
  4325. {
  4326. struct i915_vma *vma;
  4327. GEM_BUG_ON(list_empty(&o->vma_list));
  4328. list_for_each_entry(vma, &o->vma_list, obj_link) {
  4329. if (vma->is_ggtt &&
  4330. vma->ggtt_view.type == I915_GGTT_VIEW_NORMAL)
  4331. return vma->node.size;
  4332. }
  4333. return 0;
  4334. }
  4335. bool i915_gem_obj_is_pinned(struct drm_i915_gem_object *obj)
  4336. {
  4337. struct i915_vma *vma;
  4338. list_for_each_entry(vma, &obj->vma_list, obj_link)
  4339. if (vma->pin_count > 0)
  4340. return true;
  4341. return false;
  4342. }
  4343. /* Like i915_gem_object_get_page(), but mark the returned page dirty */
  4344. struct page *
  4345. i915_gem_object_get_dirty_page(struct drm_i915_gem_object *obj, int n)
  4346. {
  4347. struct page *page;
  4348. /* Only default objects have per-page dirty tracking */
  4349. if (WARN_ON((obj->ops->flags & I915_GEM_OBJECT_HAS_STRUCT_PAGE) == 0))
  4350. return NULL;
  4351. page = i915_gem_object_get_page(obj, n);
  4352. set_page_dirty(page);
  4353. return page;
  4354. }
  4355. /* Allocate a new GEM object and fill it with the supplied data */
  4356. struct drm_i915_gem_object *
  4357. i915_gem_object_create_from_data(struct drm_device *dev,
  4358. const void *data, size_t size)
  4359. {
  4360. struct drm_i915_gem_object *obj;
  4361. struct sg_table *sg;
  4362. size_t bytes;
  4363. int ret;
  4364. obj = i915_gem_object_create(dev, round_up(size, PAGE_SIZE));
  4365. if (IS_ERR(obj))
  4366. return obj;
  4367. ret = i915_gem_object_set_to_cpu_domain(obj, true);
  4368. if (ret)
  4369. goto fail;
  4370. ret = i915_gem_object_get_pages(obj);
  4371. if (ret)
  4372. goto fail;
  4373. i915_gem_object_pin_pages(obj);
  4374. sg = obj->pages;
  4375. bytes = sg_copy_from_buffer(sg->sgl, sg->nents, (void *)data, size);
  4376. obj->dirty = 1; /* Backing store is now out of date */
  4377. i915_gem_object_unpin_pages(obj);
  4378. if (WARN_ON(bytes != size)) {
  4379. DRM_ERROR("Incomplete copy, wrote %zu of %zu", bytes, size);
  4380. ret = -EFAULT;
  4381. goto fail;
  4382. }
  4383. return obj;
  4384. fail:
  4385. drm_gem_object_unreference(&obj->base);
  4386. return ERR_PTR(ret);
  4387. }